如何在sql中为无值获取零
我的问题是如何在sql中为无值获取零,sql,sql-server-2005,Sql,Sql Server 2005,我的问题是 SELECT CASE WHEN clnt_ntnlty =0 THEN 'Aboriginal' WHEN clnt_ntnlty =1 THEN 'Torres Strait Islander' WHEN clnt_ntnlty =2 THEN 'Both Aboring & Torres Strait' WHEN clnt_ntnlty =3 THEN 'Neither Aboring OR Torres Strait' ELSE 'Not Provided' END
SELECT CASE
WHEN clnt_ntnlty =0 THEN 'Aboriginal'
WHEN clnt_ntnlty =1 THEN 'Torres Strait Islander'
WHEN clnt_ntnlty =2 THEN 'Both Aboring & Torres Strait'
WHEN clnt_ntnlty =3 THEN 'Neither Aboring OR Torres Strait'
ELSE 'Not Provided' END AS Identy,
ISNULL(COUNT(clnt_ntnlty), 0) AS Counts
FROM dbo.clientInfo
group by clnt_ntnlty
我得到的结果是
如果没有值,我希望所有(五行)记录都为零。我尝试过使用group by all,但它不起作用。您可以创建一个子查询,其中包含四个您想要的值,然后对其执行
左连接
SELECT a.Identy,
COALESCE(b.Counts, 0) Counts
FROM (
SELECT 'Aboriginal' Identy
UNION ALL
SELECT 'Torres Strait Islander' Identy
UNION ALL
SELECT 'Both Aboring & Torres Strait' Identy
UNION ALL
SELECT 'Neither Aboring OR Torres Strait' Identy
) a LEFT JOIN
(
SELECT CASE
WHEN clnt_ntnlty =0 THEN 'Aboriginal'
WHEN clnt_ntnlty =1 THEN 'Torres Strait Islander'
WHEN clnt_ntnlty =2 THEN 'Both Aboring & Torres Strait'
WHEN clnt_ntnlty =3 THEN 'Neither Aboring OR Torres Strait'
ELSE 'Not Provided' END AS Identy,
ISNULL(COUNT(clnt_ntnlty), 0) AS Counts
FROM dbo.clientInfo
group by clnt_ntnlty
) b ON a.Identy = b.Identy
请尝试:
SELECT
DISTINCT Identy,
COUNT(clnt_ntnlty) OVER (PARTITION BY Identy) Counts
FROM (
SELECT 0 Val, 'Aboriginal' Identy UNION
SELECT 1 Val, 'Torres Strait Islander' Identy UNION
SELECT 2 Val, 'Both Aboring & Torres Strait' Identy UNION
SELECT 3 Val, 'Neither Aboring OR Torres Strait' Identy
) x left join dbo.clientInfo t on t.clnt_ntnlty=x.Val
如果我正确理解你的问题,你想在哪里数到“零”,那么这是一个简单的答案
更改-ISNULL(计数(clnt),0)作为计数
到-COUNT(ISNULL(clnt,0))作为计数
如果这不是您想要的,那么将有一点更详细的描述。好的,我不为下面的脚本感到自豪,因为它是一个肮脏的解决方案;并集所有可能的值,然后减去一以除去该加法
SELECT CASE
WHEN cte.clnt_ntnlty =0 THEN 'Aboriginal'
WHEN cte.clnt_ntnlty =1 THEN 'Torres Strait Islander'
WHEN cte.clnt_ntnlty =2 THEN 'Both Aboring & Torres Strait'
WHEN cte.clnt_ntnlty =3 THEN 'Neither Aboring OR Torres Strait'
ELSE 'Not Provided' END AS Identy,
ISNULL(COUNT(cte.clnt_ntnlty) - 1, 0) AS Counts
FROM (select ID,clnt_ntnlty from dbo.clientInfo
union select 0,'0' as clnt_ntnlty
union select 1, '1' as clnt_ntnlty
union select 2, '2' as clnt_ntnlty
union select 3, '3' as clnt_ntnlty) as cte
group by clnt_ntnlty
更改union select以匹配您的模式您是否有一个表,其中有一个FK用于国籍?Msg 156,15级,状态1,第21行关键字“VALUES”附近的语法不正确。很抱歉。。ur查询Msg 1087,级别15,状态2,第33行的错误必须声明表变量“@”。错误:Msg 207,级别16,状态1,第44行的列名“ID”无效。Msg 8155,级别16,状态2,第44行“cte”的第1列未指定任何列。已尝试。。但结果是一样的
SELECT CASE
WHEN cte.clnt_ntnlty =0 THEN 'Aboriginal'
WHEN cte.clnt_ntnlty =1 THEN 'Torres Strait Islander'
WHEN cte.clnt_ntnlty =2 THEN 'Both Aboring & Torres Strait'
WHEN cte.clnt_ntnlty =3 THEN 'Neither Aboring OR Torres Strait'
ELSE 'Not Provided' END AS Identy,
ISNULL(COUNT(cte.clnt_ntnlty) - 1, 0) AS Counts
FROM (select ID,clnt_ntnlty from dbo.clientInfo
union select 0,'0' as clnt_ntnlty
union select 1, '1' as clnt_ntnlty
union select 2, '2' as clnt_ntnlty
union select 3, '3' as clnt_ntnlty) as cte
group by clnt_ntnlty