SQL Server将一列旋转并取消三列。

时间:2016-08-31 08:47:24

标签: sql-server pivot unpivot

我正在努力使用sql server,我希望在其中转动一列并取消三列。而且我没有找到任何可以聚合的内容。

我有这种格式的数据:

UnAssigned  | Assigned   | InProgress | NotStarted | Date           | Type
----------------------------------------------------------------------------
31-08-2016  | NULL       | NULL       | NULL       | September2015  | ABC
NULL        | 28-08-2016 | NULL       | NULL       | August2015     | XYZ
NULL        | NULL       | 05-08-2016 | NULL       | JULY2015       | XYZ
NULL        | 28-08-2016 | NULL       | NULL       | August2015     | ABC
NULL        | NULL       | 05-08-2016 | NULL       | JULY2015       | ABC

我需要这种格式的输出:

ABC          | September2015 | August2015 | JULY2015 
-----------------------------------------------------
UnAssigned   |       1       |      0     |     0  
Assigned     |       0       |      1     |     0 
InProgress   |       0       |      0     |     1
NotStarted   |       0       |      0     |     0

XYZ          | September2015 | August2015 | JULY2015 
-----------------------------------------------------
UnAssigned   |       1       |      0     |     0
Assigned     |       0       |      1     |     0
InProgress   |       0       |      0     |     1
NotStarted   |       0       |      0     |     0

2 个答案:

答案 0 :(得分:0)

这并不是sql生成漂亮报告的能力,因为你可能会更好地使用excel或某种报告工具,如SSRS。 说过你可以使用这种方法: -

    DECLARE @T TABLE (UnAssigned VARCHAR(10),  Assigned VARCHAR(10),   InProgress VARCHAR(10) , NotStarted VARCHAR(10), Dt VARCHAR(20), Type VARCHAR(3))

    INSERT INTO @T VALUES
    ('31-08-2016'  , NULL       , NULL       , NULL       , 'September2015'  , 'ABC'),
    (NULL        , '28-08-2016' , NULL       , NULL       , 'August2015'     , 'XYZ'),
    (NULL        , NULL       , '05-08-2016' , NULL       , 'JULY2015'       , 'XYZ'),
    (NULL        , '28-08-2016' , NULL       , NULL       , 'August2015'     , 'ABC'),
    (NULL         , NULL   , '28-08-2016'    , NULL       , 'JULY2015'     , 'ABC')


    SELECT  dummytype type1,SRC,
            ISNULL([September2015],0) SEPTEMBER2015,ISNULL([August2015],0) AUGUST2015,ISNULL([JULY2015],0) JULY2015

    FROM
    (
    SELECT  'Unassigned' SRC,TYPE,type as dummytype,DT,CASE WHEN UNASSIGNED IS NOT NULL THEN 1 ELSE 0 END obs FROM @T
    UNION
    SELECT  'Assigned' ,TYPE,'',DT,CASE WHEN ASSIGNED IS NOT NULL THEN 1 ELSE 0 END ASSIGNED FROM @T
    UNION
    SELECT  'Inprogress',TYPE,'',DT,CASE WHEN INPROGRESS IS NOT NULL THEN 1 ELSE 0 END INPROGRESS FROM @T
    UNION
    SELECT  'Notstarted' ,TYPE,'',DT,CASE WHEN NOTSTARTED IS NOT NULL THEN 1 ELSE 0 END NOTSTARTED FROM @T
    ) s
    pivot (sum(obs) for dt in ([JULY2015],[August2015],[September2015])) pvt

ORDER BY TYPE,
    CASE WHEN left(SRC,1) = 'U' THEN 1
        WHEN  left(SRC,1) = 'A' THEN 2
        WHEN  left(SRC,1) = 'I' THEN 3
        WHEN  left(SRC,1)  = 'N' THEN 4
    END

结果

type1 SRC        SEPTEMBER2015 AUGUST2015  JULY2015
----- ---------- ------------- ----------- -----------
ABC   Unassigned             1           0           0
      Assigned               0           1           0
      Inprogress             0           0           1
      Notstarted             0           0           0
XYZ   Unassigned             0           0           0
      Assigned               0           1           0
      Inprogress             0           0           1
      Notstarted             0           0           0

答案 1 :(得分:0)

您可以取消隐藏,然后转动数据..只需使用COUNT()作为您的数据库中的聚合

DECLARE @tmpTable table 
    ([UnAssigned] DATETIME, [Assigned] DATETIME, [InProgress] DATETIME, [NotStarted] DATETIME, [Date] varchar(13), [Type] varchar(3))
;
INSERT INTO @tmpTable
    ([UnAssigned], [Assigned], [InProgress], [NotStarted], [Date], [Type])
VALUES
    ('2016-08-31', NULL, NULL, NULL, 'September2015', 'ABC'),
    (NULL, '2016-08-28', NULL, NULL, 'August2015', 'XYZ'),
    (NULL, NULL, '2016-08-05', NULL, 'JULY2015', 'XYZ'),
    (NULL, '2016-08-28', NULL, NULL, 'August2015', 'ABC'),
    (NULL, NULL, '2016-08-05', NULL, 'JULY2015', 'ABC')
;


WITH Sources AS (
    SELECT  Type, Src, Ord
    FROM    (SELECT DISTINCT Type FROM @tmpTable) t
            CROSS JOIN (VALUES (1, 'UnAssigned'), (2, 'Assigned'), (3, 'InProgress'), (4, 'NotStarted')) ca(Ord, Src)
)
SELECT  s.Type,
        s.Src,
        September2015   = ISNULL(September2015, 0),
        August2015      = ISNULL(August2015, 0),
        JULY2015        = ISNULL(JULY2015, 0)
FROM    @tmpTable t
        UNPIVOT ( StatusDate  FOR Status IN (UnAssigned,Assigned,InProgress,NotStarted) ) up 
        PIVOT   ( COUNT(Date) FOR Date   IN (September2015,August2015,JULY2015) ) p
        FULL OUTER JOIN Sources s ON s.Type = p.Type AND s.Src = p.Status
ORDER BY s.Type,
        s.Ord