我有表格: dbPratiche ,数据如下:
stato Gruppi Min (GG Flusso/Decorrenza-->Out)
A1 >10
A1 >16
A2 >06
A3 >08
A2 >06
A2 >08
A1 >10
A3 >06
我想将数据显示为:
Col A1 A2 A3
>10 2 0 0
>16 1 0 0
>06 0 2 1
>08 0 1 1
为此,我使用了枢轴表逻辑
select * from
(
select stato,[Gruppi Min (GG Flusso/Decorrenza-->Out)]
from
dbPratiche
) as tlb1
pivot
(
COUNT([Gruppi Min (GG Flusso/Decorrenza-->Out)] )
for [Gruppi Min (GG Flusso/Decorrenza-->Out)]
) pvt
但这并没有给我适当的结果。
Plz帮助我如何获得这个结果。
答案 0 :(得分:2)
SELECT [Gruppi Min (GG Flusso/Decorrenza-->Out)],SUM(CASE WHEN stato='A1' THEN 1 ELSE 0 END) as A1,
SUM(CASE WHEN stato='A2' THEN 1 ELSE 0 END) as A2,
SUM(CASE WHEN stato='A3' THEN 1 ELSE 0 END) as A3
FROM table GROUP BY [Gruppi Min (GG Flusso/Decorrenza-->Out)]
答案 1 :(得分:1)
我没有在([])中添加枢轴,这就是它无法正常工作的原因。
我查询如下:
select * from
(
select stato,[Gruppi Min (GG Flusso/Decorrenza-->Out)]
from
dbPratiche where
Compagnia ='GENERALI INA ASSITALIA'
and [Stato OUT/WIP] ='WIP'
) tlb1
pivot
(
COUNT( [Gruppi Min (GG Flusso/Decorrenza-->Out)] )
for [Gruppi Min (GG Flusso/Decorrenza-->Out)]
in([>10])
) PVT