Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
T-SQL-复制和;转置数据_Sql_Sql Server_Tsql - Fatal编程技术网

T-SQL-复制和;转置数据

T-SQL-复制和;转置数据,sql,sql-server,tsql,Sql,Sql Server,Tsql,我试图将数据从一个表复制到另一个表,同时将其转换并组合成适当的行,第二个表中有不同的列 第一次张贴。是的,这里的每个人都觉得这很简单。我已经试了几个小时来解决这个问题。我在内部没有太多的支持,在这个论坛上学到了很多东西,并通过您的其他帮助示例取得了很多成就。我非常感谢你在这方面的帮助 表1中的数据采用这种格式 Type Date Value -------------------- First 2019 1 First 2020 2 Second 2019 3 Secon

我试图将数据从一个表复制到另一个表,同时将其转换并组合成适当的行,第二个表中有不同的列

第一次张贴。是的,这里的每个人都觉得这很简单。我已经试了几个小时来解决这个问题。我在内部没有太多的支持,在这个论坛上学到了很多东西,并通过您的其他帮助示例取得了很多成就。我非常感谢你在这方面的帮助

表1中的数据采用这种格式

Type    Date  Value
--------------------
First   2019  1
First   2020  2
Second  2019  3
Second  2020  4
表2已经填充了日期行和创建的列。它正在等待表1中的值放入相应的列/行中

Date  First Second
------------------
2019    1     3
2020    2     4

您可以执行条件聚合:

select date, 
       max(case when type = 'first' then value end) as first,
       max(case when type = 'Second' then value end) as Second
from table t
group by date;
之后,您可以使用
cte

with cte as (
     select date, 
            max(case when type = 'first' then value end) as first,
            max(case when type = 'Second' then value end) as Second
     from table t
     group by date
)

update t2
      set t2.First = t1.First,
          t2.Second = t1.Second
from table2 t2 inner join
     cte t1
     on t1.date = t2.date;

使用条件聚合

select date,max(case when type='First' then value end) as First,
       max(case when type='Second' then value end) as Second  from t
   group by date

对于更新,我可能会使用两个
join
s:

update  t2
    set first = tf.value,
        second = ts.value
    from table2 t2 left join
         table1 tf
         on t2.date = tf.date and tf.type = 'First' left join
         table1 ts
         on t2.date = ts.date and ts.type = 'Second'
    where tf.date is not null or ts.date is not null;

看起来你在找一个


您可以使用string_agg函数,这与pivot@ZaymulThis的作用相同。非常感谢你。我投了弃权票,但我太新了,看不见。@MichaelStewart。你应该选择你认为最好的答案并接受这个答案。OP总是可以接受答案,不管声誉如何。这也奏效了。非常感谢你。我投了弃权票,但我太新了,没法看到。这很有效,这是一个令人难以置信的解释。非常感谢你。我投了弃权票,但我太新了,看不见。@MichaelStewart不客气。只要选择你认为最好的答案并接受它,在答案旁边打勾。无论声誉如何,OP始终可以接受答案。
 DECLARE @Table1 TABLE
    (
        [Type] NVARCHAR(100)
      , [Date] INT
      , [Value] INT
    );

DECLARE @Table2 TABLE(
[Date] int
,[First] int
,[Second] int
)

INSERT INTO @Table1 (
                          [Type]
                        , [Date]
                        , [Value]
                      )
VALUES ( 'First', 2019, 1 )
     , ( 'First', 2020, 2 )
     , ( 'Second', 2019, 3 )
     , ( 'Second', 2020, 4 );

INSERT INTO @Table2 (
                        [Date]
                    )
VALUES (2019),(2020)

--Show us what's in the tables
SELECT * FROM @Table1
SELECT * FROM @Table2

--How to pivot the data from Table 1 
SELECT * FROM   @Table1
    PIVOT (
              MAX([Value]) --Pivot on this Column
              FOR [Type] IN ( [First], [Second] ) --Make column where [Value] is in one of this
          ) AS [pvt] --Table alias

--which gives
--Date        First       Second
------------- ----------- -----------
--2019        1           3
--2020        2           4

--Using that we can update @Table2
UPDATE [tbl2]
SET [tbl2].[First] = pvt.[First]
    ,[tbl2].[Second] = pvt.[Second]
FROM   @Table1 tbl1
    PIVOT (
              MAX([Value]) --Pivot on this Column
              FOR [Type] IN ( [First], [Second] ) --Make column where [Value] is in one of this
          ) AS [pvt] --Table alias
INNER JOIN @Table2 tbl2 ON [tbl2].[Date] = [pvt].[Date]

--Results from @Table 2 after updated
SELECT * FROM @Table2

--which gives
--Date        First       Second
------------- ----------- -----------
--2019        1           3
--2020        2           4