我有一个SQL查询,用于计算属于某个组的帐户。我正在使用一个支点。虽然结果集看起来正确,但它返回所有0个计数
我的tsql代码:
select
*
from
(
select
id,
accountnumber,
group
from dbo.dataTable
)data
pivot
(
count(accountnumber),
for group IN (['@SGMLS L1'],['@SGMLS L2'],['@SGMLS L3'],['@SGMLS L4'],['@SGMLS L5'])
)
这就是结果集的样子:
id '@SGMLS L1' '@SGMLS L2' '@SGMLS L3' '@SGMLS L4' '@SGMLS L5'
0100 0 0 0 0 0
0200 0 0 0 0 0
0300 0 0 0 0 0
0400 0 0 0 0 0
0600 0 0 0 0 0
0800 0 0 0 0 0
1100 0 0 0 0 0
1200 0 0 0 0 0
1400 0 0 0 0 0
1500 0 0 0 0 0
1600 0 0 0 0 0
示例数据
id accountnumber group
---- ----------- ------------
0100 75 "@SGMLS L1 "
0200 109 "@SGMLS L1 "
0200 117 "@SGMLS L4 "
0200 125 "@SGMLS L4 "
0600 158 "@SGMLS L4 "
0200 166 "@SGMLS L1 "
0600 174 "@SGMLS L4 "
1200 364 "@SGMLS L3 "
1200 398 "@SGMLS L2 "
请帮助。
答案 0 :(得分:0)
尝试从枢轴quotes
移除column list
。
select
*
from
(
select
id,
accountnumber,
group
from dbo.dataTable
)data
pivot
(
count(accountnumber),
for group IN ([@SGMLS L1],[@SGMLS L2],[@SGMLS L3],[@SGMLS L4],[@SGMLS L5])
)piv
通过查看您的评论,您的group
列数据似乎包含double quotes
,并且在结尾space
之前有一个double quote
,如果是,则尝试此操作。
......
......
pivot
(
count(accountnumber),
for group IN (["@SGMLS L1 "],["@SGMLS L2 "],["@SGMLS L3 "],["@SGMLS L4 "],["@SGMLS L5 "])
)piv