Sql 枢轴返回 0

Sql Pivot returning 0

我有一个 sql 查询,用于统计属于特定组的帐户。我为此使用了一个枢轴。虽然结果集看起来正确,但 returns 全 0 计数

我的sql代码:

  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 " 

请帮忙。

尝试从枢轴 column list 中删除 quotes

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 并且尾随 double quote 之前有一个 space 如果是,请尝试此操作。

......
......
pivot
(
    count(accountnumber),
    for group IN (["@SGMLS L1 "],["@SGMLS L2 "],["@SGMLS L3 "],["@SGMLS L4 "],["@SGMLS L5 "])
)piv