如何更改表中的记录以命名SQL中的列?

时间:2017-03-28 07:14:33

标签: sql sql-server case-when

在SQL中,我有一个包含3列的表,包括:

ID      NLV         SGLV
m0001   27/03/2017  8
m0001   28/03/2017  3.5
m0002   28/03/2017  8
m0003   26/03/2017  12
M0001   27/03/2017  2

现在,我想编写一个SQL查询来创建一个具有以下格式的新表:

ID  SUM(SGLV)   26 (26/03/2017) 27 (27/03/2017) 28 (28/03/2017)
M0001   13.5    0               10              3.5
m0002   8       0               0               8
m0003   12      12              0               0

那么,有人可以帮助我吗?

3 个答案:

答案 0 :(得分:1)

    CREATE TABLE #Table1
        ([ID] varchar(5), [NLV] varchar(10), [SGLV] float)
    ;

    INSERT INTO #Table1
        ([ID], [NLV], [SGLV])
    VALUES
        ('m0001', '27/03/2017', 8),
        ('m0001', '28/03/2017', 3.5),
        ('m0002', '28/03/2017', 8),
        ('m0003', '26/03/2017', 12),
        ('M0001', '27/03/2017', 2)
    ;

    SELECT ID,ISNULL([26/03/2017],0)+ISNULL([27/03/2017],0)+ISNULL([28/03/2017],0) AS [SUM(DGLV)],
ISNULL([26/03/2017],0)'26 (26/03/2017)',ISNULL([27/03/2017],0)'27 (27/03/2017)',ISNULL([28/03/2017],0)'28 (28/03/2017)'
FROM 
(
  SELECT *
  FROM #TABLE1
) SRC
PIVOT
(
   SUM(SGLV)  
  FOR NLV IN ([26/03/2017], [27/03/2017],[28/03/2017])
) PIV;

答案 1 :(得分:1)

试试这可能会对你有所帮助

select * into #temp from (

select 'm0001'ID,   '27/03/2017' as NLV ,8 SGLV
union all
select 'm0001',   '28/03/2017',  3.5
union all
select 'm0002  ', '28/03/2017' , 8
union all
select 'm0003   ','26/03/2017' ,12
union all
select 'M0001   ','27/03/2017' , 2
) as a


;with cte as (
select ID,isnull([27/03/2017],0)as [27/03/2017],isnull([28/03/2017],0)as [28/03/2017],isnull([26/03/2017],0) as [26/03/2017] from(
select * from #temp
) as a
pivot
(
 max(sglv) for nlv in ([27/03/2017],[28/03/2017],[26/03/2017])
)
as a
)
select id,[27/03/2017]+[28/03/2017]+[26/03/2017] as SUM_SGLV,[27/03/2017],[28/03/2017],[26/03/2017] from cte

答案 2 :(得分:1)

在SQL Server中,下面的查询为您提供所需的输出

DECLARE @SAMPLEDATA TABLE(ID VARCHAR(100),     NLV  DATETIME,       SGLV DECIMAL(18,1))
INSERT INTO @SAMPLEDATA VALUES
('m0001',   '03/27/2017',  8),
('m0001',   '03/28/2017',  3.5),
('m0002',   '03/28/2017',  8),
('m0003',   '03/26/2017',  12),
('M0001',   '03/27/2017',  2)

SELECT ID,ISNULL([03/26/2017],0)[03/26/2017],ISNULL([03/27/2017],0)    [03/27/2017],ISNULL([03/28/2017],0)[03/28/2017] FROM
(
    SELECT ID,NLV,SGLV FROM @SAMPLEDATA
)T
PIVOT
(
    SUM(SGLV) FOR NLV IN([03/26/2017],[03/27/2017],[03/28/2017])
)PIV;

输出

------------------------------------------
--ID    03/26/2017  03/27/2017  03/28/2017
------------------------------------------
m0001   0.0         10.0        3.5
m0002   0.0         0.0         8.0
m0003   12.0        0.0         0.0
------------------------------------------

使用DYNAMIC QUERY :正如评论中所提到的,如果需要为当前完整的月份生成输出,那么使用动态查询也可以实现相同,而不是每天都使用CASE编写CASE。月。请尝试以下查询:

DECLARE @DYNAMICQUERY NVARCHAR(MAX)=''
DECLARE @DATERANGE VARCHAR(MAX)=''
;WITH COMPLETEMONTH (COL)
AS
(
        SELECT CAST(CAST(MONTH(CURRENT_TIMESTAMP) AS     VARCHAR(2))+'/'+'01/'+CAST(YEAR(CURRENT_TIMESTAMP) AS VARCHAR(4)) AS DATETIME)
            UNION ALL
        SELECT DATEADD(D,1,COL)COL FROM COMPLETEMONTH 
            WHERE COL< DATEADD(D,-1,    (DATEADD(M,1,CAST(CAST(MONTH(CURRENT_TIMESTAMP) AS     VARCHAR(2))+'/'+'01/'+CAST(YEAR(CURRENT_TIMESTAMP) AS VARCHAR(4)) AS     DATETIME))))
)
SELECT @DATERANGE=@DATERANGE+'['+CONVERT(VARCHAR,COL,101)+']'+',' FROM     COMPLETEMONTH
SET @DATERANGE=LEFT(@DATERANGE,LEN(@DATERANGE)-1)

SELECT @DYNAMICQUERY=
'SELECT ID,'+@DATERANGE+' FROM
(
    SELECT ''m0001'' ID ,   ''03/27/2017'' NLV,  8 SGLV
    UNION ALL 
    SELECT ''m0001'',   ''03/28/2017'',  3.5
    UNION ALL 
    SELECT ''m0002'',   ''03/28/2017'',  8
    UNION ALL 
    SELECT ''m0003'',   ''03/26/2017'',  12
    UNION ALL 
    SELECT ''M0001'',   ''03/27/2017'',  2
)T
PIVOT
(
    SUM(SGLV) FOR NLV IN('+@DATERANGE+')
)PIV;'

EXEC SP_EXECUTESQL @DYNAMICQUERY,N'@DATERANGE     VARCHAR(MAX)',@DATERANGE=@DATERANGE