Oracle“ table1”
number code
111 aaa
222 bbb
333 aaa
查询
SELECT COUNT(*) AS CNT, CODE
FROM TABLE1
WHERE NUMBER IN (
'444',
'111',
'333',
'222'
) GROUP BY CODE;
答案
CNT CODE
2 aaa
1 bbb
需要结果,请帮忙。
CNT CODE
1 null
2 aaa
1 bbb
我尝试使用“ nvl”请求,但它仅返回(计数)具有(空)代码的行,但不计数行。
PS。对不起,英语不好:)
答案 0 :(得分:2)
我认为您想要left join
:
SELECT CODE, COUNT(t1.NUMBER) AS CNT
FROM (SELECT '444' as num FROM DUAL UNION ALL
SELECT '111' as num FROM DUAL UNION ALL
SELECT '333' as num FROM DUAL UNION ALL
SELECT '222' as num FROM DUAL
) x LEFT JOIN
TABLE1 t1
ON t1.NUMBER = x.num
GROUP BY CODE;