从PIVOT获得结果后的GROUP BY

时间:2016-03-19 19:13:25

标签: sql sql-server

我正在寻找一个方法小组empNumber,似乎无法找到方法

这是我目前的查询:

SELECT   EmpNumber, [Match], [NonMatch], [Base], [SRA], [SRS]

from 

(select a.empnumber, TT_GB.MAXDate, a.TT_Plan, a.TT_Ratio
from table_1 as a

inner join

(select b.empnumber, b.TT_Plan, MAX(b.TT_Date) as MAXDate
from table_1 as b
group by b.empnumber, b.TT_Plan) TT_GB

ON a.EmpNumber = TT_GB.EmpNumber AND a.TT_Plan = TT_GB.TT_Plan AND a.TT_Date = TT_GB.MAXDate 
) M

PIVOT
(
       SUM(TT_Ratio)
       FOR [TT_Plan] IN ([Match], [NonMatch], [Base], [SRA], [SRS])
) P

这就是我在表格中的内容

=============================================
EmpNumber   TT_Plan     TT_Date     TT_Ratio
1           Base        2008-05-11  11
1           Base        2015-08-22  12
1           Base        2010-07-09  13
1           Match       2003-01-23  15
1           Match       2000-11-17  14
1           NonMatch    2014-09-22  19
1           NonMatch    2015-02-15  18
1           NonMatch    2009-08-13  17
1           SRA         2005-07-16  2
1           SRS         2006-04-12  3
2           Base        2008-08-08  5
2           Base        2009-09-09  7
2           Base        2010-10-10  4
2           NonMatch    2011-01-01  8
2           NonMatch    2012-12-12  6
2           NonMatch    2007-07-07  9

Desired output
==============
EmpNumber   Match   NonMatch    Base       SRA    SRS
1           15        18         12         2      3
2            0         6          4         0      0 

但我现在所拥有的就是这个,我想要的就是按照上面的EmpNumber分组

=============================================================================
EmpNumber   Match   NonMatch    Base        SRA     SRS
1           15      NULL        NULL        NULL    NULL
1           NULL    NULL        NULL        2       NULL
1           NULL    NULL        NULL        NULL    3
2           NULL    NULL        4           NULL    NULL
2           NULL    6           NULL        NULL    NULL
1           NULL    18          NULL        NULL    NULL
1           NULL    NULL        12          NULL    NULL

3 个答案:

答案 0 :(得分:1)

我不认为这是最好的解决方案,但你可以通过创建如下的别名表来实现:

Select t.EmpNumber, Sum(t.Match), Sum(t.NonMatch), Sum(t.Base), Sum(t.SRA), Sum(t.SRS) from
(
SELECT   EmpNumber, [Match], [NonMatch], [Base], [SRA], [SRS]

from 

(select a.empnumber, TT_GB.MAXDate, a.TT_Plan, a.TT_Ratio
from table_1 as a

inner join

(select b.empnumber, b.TT_Plan, MAX(b.TT_Date) as MAXDate
from table_1 as b
group by b.empnumber, b.TT_Plan) TT_GB

ON a.EmpNumber = TT_GB.EmpNumber AND a.TT_Plan = TT_GB.TT_Plan AND a.TT_Date = TT_GB.MAXDate 
) M

PIVOT
(
       SUM(TT_Ratio)
       FOR [TT_Plan] IN ([Match], [NonMatch], [Base], [SRA], [SRS])
) P
) t group by t.EmpNumber

答案 1 :(得分:1)

所以我之前写的是

declare @table_1 table ( EmpNumber int,   TT_Plan varchar(20),    TT_Date date,    TT_Ratio int )
insert @table_1
values 
(1,'Base','2008-05-11',11)
,(1,'Base','2015-08-22',12)
,(1,'Base','2010-07-09',13)
,(1,'Match','2003-01-23',15)
,(1,'Match','2000-11-17',14)
,(1,'NonMatch','2014-09-22',19)
,(1,'NonMatch','2015-02-15',18)
,(1,'NonMatch','2009-08-13',17)
,(1,'SRA','2005-07-16',2)
,(1,'SRS','2006-04-12',3)
,(2,'Base','2008-08-08',5)
,(2,'Base','2009-09-09',7)
,(2,'Base','2010-10-10',4)
,(2,'NonMatch','2011-01-01',8)
,(2,'NonMatch','2012-12-12',6)
,(2,'NonMatch','2007-07-07',9)

SELECT   EmpNumber, [Match], [NonMatch], [Base], [SRA], [SRS]

from 

(select a.empnumber,a.TT_Plan, a.TT_Ratio
from @table_1 as a

inner join

(select b.empnumber, b.TT_Plan, MAX(b.TT_Date) as MAXDate
from @table_1 as b
group by b.empnumber, b.TT_Plan) TT_GB

ON a.EmpNumber = TT_GB.EmpNumber AND a.TT_Plan = TT_GB.TT_Plan AND a.TT_Date = TT_GB.MAXDate 
) M

PIVOT
(
       SUM(TT_Ratio)
       FOR [TT_Plan] IN ([Match], [NonMatch], [Base], [SRA], [SRS])
) P

结果是

EmpNumber   Match       NonMatch    Base        SRA         SRS
----------- ----------- ----------- ----------- ----------- -----------
1           15          18          12          2           3
2           NULL        6           4           NULL        NULL

答案 2 :(得分:1)

您可以使用窗口功能首先过滤每组的最新记录。

然后工作查询变为:

select *
from (
    select empnumber, tt_plan, tt_ratio
    from   (
          select   *,
                   rank() over (partition by empnumber, tt_plan 
                                order by tt_date desc) as rnk
          from     table_1) as t1
    where  rnk = 1) as m
pivot (
       sum(TT_Ratio)
       for TT_Plan in ([Match], [NonMatch], [Base], [SRA], [SRS])
) as p

SQL fiddle

输出:

+-----------+--------+----------+------+--------+--------+
| empnumber | Match  | NonMatch | Base |  SRA   |  SRS   |
+-----------+--------+----------+------+--------+--------+
|    1      |    15  |    18    |  12  |    2   |    3   |
|    2      | (null) |     6    |   4  | (null) | (null) |
+-----------+--------+----------+------+--------+--------+