--Sql中 Count 统计的记录 为 空时 如何显示 0 ?
SNO为会员ID,积分val的记录如下,需要完成对val积分123456789及包含10以上的积分的统计
,例如:积分5记录 为 空时 如何显示 0 ?,执行下面的脚本,积分5记录 为 空时 显示应该是0,实际上结果里没有.请专家答疑解惑,谢谢
;WITH T AS(
SELECT '2022160'SNO,'2'val
UNION ALL SELECT '2022160','7'
UNION ALL SELECT '2022160','8'
UNION ALL SELECT '2022160','8'
UNION ALL SELECT '2022160','2'
UNION ALL SELECT '2022160','8'
UNION ALL SELECT '2022160','4'
UNION ALL SELECT '2022160','9'
UNION ALL SELECT '2022160','9'
UNION ALL SELECT '2022160','6'
UNION ALL SELECT '2022160','3'
UNION ALL SELECT '2022160','1'
UNION ALL SELECT '2022160','3'
UNION ALL SELECT '2022160','6'
UNION ALL SELECT '2022160','4'
UNION ALL SELECT '2022160','10'
UNION ALL SELECT '2022160','2'
UNION ALL SELECT '2022160','3'
UNION ALL SELECT '2022160','4'
UNION ALL SELECT '2022160','2'
)
SELECT SNO,COUNT(VAL) AS WM_RpNr from T where val=1 GROUP BY SNO,VAL
UNION ALL
SELECT SNO,COUNT(VAL) AS WM_RpNr from T where val=2 GROUP BY SNO,VAL
UNION ALL
SELECT SNO,COUNT(VAL) AS WM_RpNr from T where val=3 GROUP BY SNO,VAL
UNION ALL
SELECT SNO,COUNT(VAL) AS WM_RpNr from T where val=4 GROUP BY SNO,VAL
UNION ALL
SELECT SNO,COUNT(VAL) AS WM_RpNr from T where val=5 GROUP BY SNO,VAL
UNION ALL
SELECT SNO,COUNT(VAL) AS WM_RpNr from T where val=6 GROUP BY SNO,VAL
UNION ALL
SELECT SNO,COUNT(VAL) AS WM_RpNr from T where val=7 GROUP BY SNO,VAL
UNION ALL
SELECT SNO,COUNT(VAL) AS WM_RpNr from T where val=8 GROUP BY SNO,VAL
UNION ALL
SELECT SNO,COUNT(VAL) AS WM_RpNr from T where val=9 GROUP BY SNO,VAL
UNION ALL
SELECT SNO,COUNT(VAL) AS WM_RpNr from T where val>=10 GROUP BY SNO,VAL
想要得到的结果
----------------------------------------------------------------------
SNO WM_RpNr
2022160 1
2022160 4
2022160 3
2022160 3
2022160 0
2022160 2
2022160 1
2022160 3
2022160 2
2022160 1