枢轴函数返回空值

时间:2019-02-28 09:19:46

标签: sql sql-server sql-server-2012

我想在第一行中显示所有三行,所以我在查询中使用了数据透视 **附有输出图片** enter image description here

SELECT [Container Number],ReadTimee,Locationn,Trailer FROM             
(SELECT [Container Number],ReadTime,Location FROM #temp )as Tab1            
PIVOT            
(            
MAX(ReadTime) FOR Location IN (Trailer,ReadTimee,Locationn)) AS Tab2  

但是使用上面的查询返回空值 。输出图片如下所示 enter image description here

我想要这样的输出

ReadTime                Location    Trailer Container Number           ReadTime                 Location    Trailer Container Number              ReadTime                  Location   Trailer  Container Number
2019-02-27 03:17:21.033 CUSTOM         1    ZIMU1374787,TRHU3437713   2019-02-27 06:10:35.470          ZERO        1    ZIMU1374787,TRHU3437713 2019-02-27 07:30:47.407             CFS            1    ZIMU1374787,TRHU3437713

2 个答案:

答案 0 :(得分:2)

尝试一下:

SELECT Trailer, [Container Number], [CUSTOM] = MAX(CASE WHEN Location = 'CUSTOM' THEN ReadTime ELSE NULL END),
    [ZERO] = MAX(CASE WHEN Location = 'ZERO' THEN ReadTime ELSE NULL END),
    [CFS] = MAX(CASE WHEN Location = 'CFS' THEN ReadTime ELSE NULL END)
FROM @tmp 
GROUP BY Trailer, [Container Number]

以上查询返回:

Trailer Container Number        CUSTOM                  ZERO                    CFS
1       ZIMU1374787,TRHU3437713 2019-02-27 03:17:21.033 2019-02-27 06:10:35.470 2019-02-27 07:30:47.407

如果我的答案符合您的需求,请接受我的答案。随时根据您的进一步需求进行改进。

答案 1 :(得分:2)

您可以使用MAX方法获取必要的列:

SELECT
  MAX(CASE WHEN q.RowNumber = 1 THEN q.ReadTime ELSE NULL END) AS ReadTime
, MAX(CASE WHEN q.RowNumber = 1 THEN q.Location ELSE NULL END) AS Location
, MAX(CASE WHEN q.RowNumber = 1 THEN q.Trailer ELSE NULL END) AS Trailer
, MAX(CASE WHEN q.RowNumber = 1 THEN q.ContainerNumber ELSE NULL END) AS ContainerNumber
, MAX(CASE WHEN q.RowNumber = 2 THEN q.ReadTime ELSE NULL END) AS ReadTime
, MAX(CASE WHEN q.RowNumber = 2 THEN q.Location ELSE NULL END) AS Location
, MAX(CASE WHEN q.RowNumber = 2 THEN q.Trailer ELSE NULL END) AS Trailer
, MAX(CASE WHEN q.RowNumber = 2 THEN q.ContainerNumber ELSE NULL END) AS ContainerNumber
, MAX(CASE WHEN q.RowNumber = 3 THEN q.ReadTime ELSE NULL END) AS ReadTime
, MAX(CASE WHEN q.RowNumber = 3 THEN q.Location ELSE NULL END) AS Location
, MAX(CASE WHEN q.RowNumber = 3 THEN q.Trailer ELSE NULL END) AS Trailer
, MAX(CASE WHEN q.RowNumber = 3 THEN q.ContainerNumber ELSE NULL END) AS ContainerNumber
FROM
(
    SELECT 
    *
    , ROW_NUMBER() OVER(PARTITION BY T.Trailer ORDER BY T.Trailer) AS RowNumber
    FROM @Table AS T
)AS q

让我展示一个例子:

DECLARE @Table TABLE
(
    ReadTime DATETIME,
    [Location] VARCHAR(50),
    Trailer INT,
    ContainerNumber VARCHAR(50)
)

INSERT INTO @Table
(
    ReadTime,
    Location,
    Trailer,
    ContainerNumber
)
VALUES
  (   '2019-02-23 12:22:35.490', 'CUSTOM', 1, 'ZIMU1' )
, (   '2019-02-24 12:22:35.490', 'ZERO', 1, 'ZIMU2' )
, (   '2019-02-25 12:22:35.490', 'CFS', 1, 'ZIMU3')

并使用上面的查询获取所需的输出:

enter image description here