我正在尝试使用这些查询进行计数。我希望它显示我所有的名字主持人(虽然计数是0)但是使用这些查询,计数为0的nameHost不显示nameHost。 你能帮帮我吗?
select a.nameHost, count(b.job_name) as cuenta
from jobsDefinition b
right join cmdmzpre.nodes a
on b.node_id in (a.nodeid,a.nameHost)
where b.app not like 'UNPLAN' group by a.nameHost order by a.nameHost desc;
表格示例:
nodes
=======
nameHost nodeid
--------- -------
a a
b b
b f
e g
jobsDefinition
================
node_id job_name
---------- -----------
a fruit
b apple
c iron
a banana
f orange
输出结果为:
a 2 (fruit,banana)
b 2 (apple,orange)
e 0
答案 0 :(得分:0)
正如我的评论中提到的,From your dataset, **a** should be **2**.
使用此查询:
---Table Data start ----
WITH nodes (nameHost, nodeid)
AS (SELECT 'a', 'a' FROM DUAL
UNION ALL
SELECT 'b', 'b' FROM DUAL
UNION ALL
SELECT 'b', 'f' FROM DUAL
UNION ALL
SELECT 'e', 'g' FROM DUAL),
jobdef (node_id, job_name)
AS (SELECT 'a', 'fruit' FROM DUAL
UNION ALL
SELECT 'b', 'apple' FROM DUAL
UNION ALL
SELECT 'c', 'iron' FROM DUAL
UNION ALL
SELECT 'a', 'banana' FROM DUAL
UNION ALL
SELECT 'f', 'orange' FROM DUAL)
--Table data end---
--Actual Query
SELECT n.namehost,
COUNT (jd.node_id) AS Cnt,
LISTAGG (jd.job_name, ',') WITHIN GROUP (ORDER BY 1) JB_NM
FROM nodes n
LEFT JOIN jobdef jd
ON n.nodeid = jd.node_id
GROUP BY n.namehost
ORDER BY namehost;