[英]DB2 SQL: How to 'count' the amount of records returned by a having clause
Table XRDK/WHSHIPP_R3 has 4 columns - ZNWHSE, ZNSITE, ZNMANE, ZNRECD with a total of 1,071 records. 表XRDK / WHSHIPP_R3有4列--ZNWHSE,ZNSITE,ZNMANE,ZNRECD,共有1,071条记录。
I have isolated the ZNMANE numbers that have been used more than once by; 我已经隔离了多次使用过的ZNMANE号码;
SELECT ZNMANE FROM XRDK/WHSHIPP_R3
GROUP BY ZNMANE
HAVING (COUNT(ZNMANE) >1)
ORDER BY 1
I would like a total count of these isolated records, but if I change it to; 我想要这些孤立记录的总数,但是如果我改成它;
SELECT COUNT(ZNMANE) FROM XRDK/WHSHIPP_R3
GROUP BY ZNMANE
HAVING (COUNT(ZNMANE) >1)
ORDER BY 1
I just get a load of 2s which must be the individual count for each ZNMANE record. 我只需要加载2s,这必须是每个ZNMANE记录的单独计数。
I tried this; 我试过这个;
SELECT ZNMANE FROM XRDK/WHSHIPP_R3
GROUP BY ZNMANE
HAVING (COUNT(ZNMANE) >1)
ORDER BY 1
UNION ALL
SELECT COUNT(ZNMANE) FROM XRDK/WHSHIPP_R3
But this returned 1071 at the top, so I guess it just counted the whole file. 但这在顶部返回1071,所以我猜它只计算整个文件。 Any ideas?
有任何想法吗?
On method is a subquery: 方法是子查询:
SELECT COUNT(*)
FROM (SELECT ZNMANE
FROM XRDK/WHSHIPP_R3
GROUP BY ZNMANE
HAVING COUNT(ZNMANE) > 1
) z
If you want the value in each row, use window functions: 如果您想要每行中的值,请使用窗口函数:
SELECT ZNMANE, COUNT(*) OVER () as NumTotal
FROM XRDK/WHSHIPP_R3
GROUP BY ZNMANE
HAVING COUNT(ZNMANE) > 1
A group by usually needs a criteria (what you want to group by, ZNMANE in this case) and the aggregate (COUNT in this case). 组通常需要一个条件(您要分组的内容,在这种情况下为ZNMANE)和聚合(在这种情况下为COUNT)。
I think your first query should look more look like 我认为你的第一个查询应该看起来更像
SELECT ZNMANE
, COUNT(1)
FROM XRDK/WHSHIPP_R3
GROUP
BY ZNMANE
HAVING (COUNT(1) > 1)
ORDER
BY 1
You can try this and see if you get what you need.. 你可以尝试这个,看看你是否得到了你需要的东西..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.