我有以下查询:\
SELECT *
FROM (SELECT 1 CntCol, CONVERT(CHAR(3), [data out (no val#vuoto)], 0) month,
( [gruppi min (gg flusso/decorrenza-->out)] )
FROM dbpratiche
WHERE compagnia = 'GENERALI INA ASSITALIA'
AND stato = 'OUT ATTIVATA'
AND [data out (no val#vuoto)] > '01-01-2012') TLB
PIVOT ( Count(CntCol)
FOR tlb.month IN([nov],
[dec],
[jan],
[feb],
[mar],
[apr],
[May],
[jun],
[jul],
[aug],
[sep],
[oct] ) )PVT
此pivotote查询产生以下结果:
我想再次调整此表格以获得结果:
Months <=06 <=8 <=10 >10
Jan 0 1 1 6
Feb 8 5 4 2
Mar 9 7 6 2
请帮忙。
答案 0 :(得分:3)
试试这段代码:
SELECT *
FROM (SELECT 1 CntCol, CONVERT(CHAR(3), [data out (no val#vuoto)], 0) month,
( [gruppi min (gg flusso/decorrenza-->out)] )
FROM dbpratiche
WHERE compagnia = 'GENERALI INA ASSITALIA'
AND stato = 'OUT ATTIVATA'
AND [data out (no val#vuoto)] > '01-01-2012') TLB
PIVOT ( Count(CntCol)
FOR tlb.[gruppi min (gg flusso/decorrenza-->out)] IN
([<=06],
[<=08],
[<=10],
[>10] ) )PVT
答案 1 :(得分:1)
我自己解决了这个问题:
select * from
(
SELECT 1 CntCol, CONVERT(CHAR(3), [data out (no val#vuoto)], 0) month,
( [gruppi min (gg flusso/decorrenza-->out)] )
FROM dbpratiche
WHERE compagnia = 'GENERALI INA ASSITALIA'
AND stato = 'OUT ATTIVATA'
AND [data out (no val#vuoto)] > '01-01-2012'
) T
pivot
(
count(cntcol)
for [gruppi min (gg flusso/decorrenza-->out)]
in([>10],[<=10],[<=06],[<=08])
)P
答案 2 :(得分:1)
你似乎想要的是让枢轴反过来。我建议你试试这个:
SELECT *
FROM (SELECT 1 CntCol, CONVERT(CHAR(3), [data out (no val#vuoto)], 0) month,
( [gruppi min (gg flusso/decorrenza-->out)] as gg)
FROM dbpratiche
WHERE compagnia = 'GENERALI INA ASSITALIA'
AND stato = 'OUT ATTIVATA'
AND [data out (no val#vuoto)] > '01-01-2012') TLB
PIVOT ( Count(CntCol)
FOR tlb.gg IN([<=06],
[<=08],
[<=10],
[>10]
) )PVT
如果您的列中包含的值超过了上述值,请查看动态sql,如here