这是我的表cash_main:
“CASH_MAIN”表有两列,如下所示:
CASHID Amount
7 1000
9 300
Deatils将出现在“cash_detail”表中:
cash_detailid cashID agentid Amount
1 7 101 200
2 7 102 200
3 7 103 200
4 7 104 200
5 7 105 200
6 9 106 300
我正在尝试在详细信息表中获取具有多个agentid链接的cashid:
cashid
7
这里的示例cashid:7有多个代理ID,总计为1000 但是我们不应该选择Cashid:9因为它只有一个单一的年龄并且有300个
我正在尝试使用下面的查询,但它没有取得正确的结果:
WITH Duplicateaccounts
AS (
SELECT CASHID
,count(*) AS rcount
FROM cash_detail
GROUP BY CASHID
HAVING count(*) > 1
)
SELECT CASHID
FROM cash_detail a
JOIN cash_MAIN b ON b.CASHID = a.CASHID
ORDER BY a.cashid
答案 0 :(得分:3)
您可以使用group by
和having
:
select cd.cashID
from cash_detail cd
group by cd.cashID
having min(agentid) <> max(agentid);
答案 1 :(得分:0)
COUNT(DISTINCT)可用于识别特定字段中不同值的位置。
SELECT cash_detail.cashID
FROM cash_detail
GROUP BY cash_detail.cashID
HAVING COUNT(DISTINCT cash_detail.agentid)>1;