我对SQL非常陌生,遇到了这个问题。

CREATE TABLE #TempTable
(WeekNr int,
Name varchar(30),
Value int)

INSERT INTO #TempTable VALUES (21,'John',100)
INSERT INTO #TempTable VALUES (21,'Michael',133)
INSERT INTO #TempTable VALUES (21,'Tony',50)
INSERT INTO #TempTable VALUES (22,'John',80)
INSERT INTO #TempTable VALUES (23,'Michael',188)
INSERT INTO #TempTable VALUES (23,'Tony',230)


表显示了这种方式:

  WeekNr         Name        Value
  21             John        100
  21             Michael     133
  21             Tony        50
  22             John        80
  23             Michael     188
  23             Tony        230


我需要以这种方式安排数据:

  Name           Vale          Name         Value        Name        Value
  John           100           John          80          Michael     188
  Michael        133                                     Tony        230
  Tony           50


其中前两列表示WeekNr为21,后两列表示WeekNr为22,然后WeekNr为23。

如果将Null值包括在内,则最好不跳过任何WeekNr。例如,如果我做类似的事情

  DELETE FROM #TempTable WHERE WeekNr = 22


它应显示为:

  Name           Vale          Name         Value        Name        Value
  John           100                                     Michael     188
  Michael        133                                     Tony        230
  Tony           50


因此很容易将数据导出到Excel。
我需要使用PIVOT还是UNPIVOT?

最佳答案

试试这个。

SELECT Max([21]) NAME,Max([id21]) Value,Max([22]) Name,Max([id22]) Value,Max([23]) Name,Max([id23]) Value
FROM   (SELECT Row_number()OVER(partition BY WeekNr ORDER BY NAME) rn,
               NAME,value,WeekNr,
               'id' + CONVERT(VARCHAR, WeekNr)      AS weeks
        FROM   #TempTable) a
       PIVOT (Max(NAME)
             FOR WeekNr IN ([21],[22],[23])) piv
       PIVOT (max(value)
             FOR weeks IN ([id21],[id22],[id23])) piv1
GROUP BY rn


如果您希望代码动态运行,请尝试此操作。

DECLARE @cols     VARCHAR(max)='',
        @cols1    VARCHAR(max)='',
        @aggcols  VARCHAR(max)='',
        @aggcols1 VARCHAR(max)='',
        @sql      NVARCHAR(max)

SELECT @cols += ',[' + CONVERT(VARCHAR(30), weeknr)+']',
       @cols1 += ',[id' + CONVERT(VARCHAR(30), weeknr)+']',
       @aggcols += ',max([' + CONVERT(VARCHAR(30), weeknr)+ ']) Name',
       @aggcols1 += ',max([id' + CONVERT(VARCHAR(30), weeknr)+ ']) Value'
FROM   (SELECT DISTINCT WeekNr
        FROM   #TempTable) A

select @cols= RIGHT(@cols,len(@cols)-1)
select @cols1= RIGHT(@cols1,len(@cols1)-1)

select @aggcols= RIGHT(@aggcols,len(@aggcols)-1)
select @aggcols1= RIGHT(@aggcols1,len(@aggcols1)-1)

set @sql ='SELECT '+@aggcols+','+@aggcols1+'
FROM   (SELECT Row_number()OVER(partition BY WeekNr ORDER BY NAME) rn,
               NAME,value,WeekNr,
               ''id'' + CONVERT(VARCHAR, WeekNr)      AS weeks
        FROM   #TempTable) a
       PIVOT (Max(NAME)
             FOR WeekNr IN ('+@cols+')) piv
       PIVOT (max(value)
             FOR weeks IN ('+@cols1+')) piv1
GROUP BY rn'


exec sp_executesql @sql

关于sql - 显示具有不同值(和空值)的相同列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26999178/

10-08 22:42