如何使用case语句在PIVOT中获取多列

时间:2019-03-15 09:44:06

标签: sql sql-server

当前代码为

select tab.Name, count(*) as count, newtype, sum(tab.area) as area
from 
 (Select b.ID,b.Name,b.area
 , case when a.type='R' then 'R'
   when a.type='P' then 'P' else 'V' end newtype
from table1 a
left join 
(
    select * from table2) b on a.ID=b.ID) as tab
group by newtype, tab.Name

我能够获取数据透视图中1列的结果,而对于多个值却无法做到这一点

当前输出

+------+-------+------+-------------+
| **Name | count | type |    area**     |
+------+-------+------+-------------+
| BC   |  1791 | P    | 8.49088929  |
| Ko   |  2037 | V    | 6.00518816  |
| La   |  9770 | V    | 23.94630601 |
| Mu   |  2954 | P    | 8.76069522  |
| TR   |  4883 | V    | 22.49431638 |
| BC   |   253 | R    | 0.94008881  |
| Ko   |  4264 | V    | 18.62079158 |
| La   |    77 | R    | 0.08762128  |
| Mu   |  1108 | R    | 3.51179297  |
| TR   |  5388 | P    | 25.59107397 |
| BC   |  4944 | V    | 11.22571974 |
| Ko   |  3110 | P    | 29.61730073 |
| La   |   265 | R    | 0.52175506  |
| Mu   |  2992 | P    | 26.78753297 |
| TR   |    97 | R    | 0.29497479  |
+------+-------+------+-------------+

所需结果

+-------------+-------+-------------+-------+------------+-------+-------------+
|    Name     |   R   |             |   V   |            |   P   |             |
+-------------+-------+-------------+-------+------------+-------+-------------+
|             | count | area        | count | area       | count | area 
+-------------+-------+-------------+-------+------------+-------+-------------+       
| BC          | 3110  | 29.61730073 | 1108  | 3.51179297 | 4264  | 18.62079158 |
| Ko          | 1791  | 8.49088929  | 97    | 0.29497479 | 2037  | 6.00518816  |
| La          | 2954  | 8.76069522  | 265   | 0.52175506 | 4944  | 11.22571974 |
| Mu          | 2992  | 26.78753297 | 253   | 0.94008881 | 4883  | 22.49431638 |
| TR          | 5388  | 25.59107397 | 77    | 0.08762128 | 9770  | 23.94630601 |
+-------------+-------+-------------+-------+------------+-------+-------------+
| Grand Total | 16235 | 99.24749218 | 1800  | 5.35623291 | 25898 | 82.29232187 |
+-------------+-------+-------------+-------+------------+-------+-------------+

相同类型的区域和计数需要作为枢轴。

2 个答案:

答案 0 :(得分:0)

您可以在下面尝试-

with cte as
(
select tab.Name, count(*) as count, newtype, sum(tab.area) as area
from 
(Select b.ID,b.Name,b.area, 
case when a.type='R' then 'R' when a.type='P' then 'P' else 'V' end newtype
from table1 a
left join 
(select * from table2) b on a.ID=b.ID) as tab
group by newtype, tab.Name
)

select name, max(case when type='P' then countval end) as Pcount,
max(case when type='P' then area end) as PArea,
max(case when type='V' then countval end) as Vcount,
max(case when type='V' then area end) as VArea,
max(case when type='R' then countval end) as Rcount,
max(case when type='R' then area end) as RArea
from cte 
group by name

答案 1 :(得分:0)

您可以执行此操作而无需子查询:

select a.name,
       sum(case when v.newtype = 'R' then 1 else 0 end) as r_count,
       sum(case when v.newtype = 'R' then b.area else 0 end) as r_area,
       sum(case when v.newtype = 'V' then 1 else 0 end) as v_count,
       sum(case when v.newtype = 'V' then b.area else 0 end) as v_area,
       sum(case when v.newtype = 'P' then 1 else 0 end) as p_count,
       sum(case when v.newtype = 'P' then b.area else 0 end) as p_area
from table1 a cross apply
     (values (case when a.type in ('R', 'P') then a.type
                   else 'V'
              end)
     ) v(a.newtype) left join
     table2 b
     on a.ID = b.ID
group by group by grouping sets ( (a.name), () );

其中的关键部分是grouping sets构造,用于计算总计。