我的查询有问题,请参阅我有两个表,让我们说:
table a:
progid | name | type
12 | john | b
12 | anna | c
13 | sara | b
13 | ben | c
14 | alan | b
15 | george| b
table b:
progid | name | type
12 | john | b
12 | anna | c
13 | sara | b
14 | alan | b
15 | george| b
表a得到计数
progid | count(*)
12 | 2
13 | 2
14 | 1
15 | 1
表b获取
progid | count(*)
12 | 2
**13 | 1**<-this is what I want to find different count
14 | 1
15 | 1
我想要的是找到表b中的哪个progid不在表a中的计数,(因为你可以看到prog id在那里,但它们应该在同一时间!所以ben已经消失但是progid 13是那里)
所以我想得到表格中数量不同的progid,我试过:
select a.progid from
(select progid ,count(*) total from tablea group by progid) a,
(select progid ,count(*) total from tableb group by progid) b
where
a.progid=b.progid and a.total<>b.total;
我得到b.total无效标识符
if I use a.count(progid)<>b.count(progid)
错误说不能在那里使用组功能,有什么想法吗?我很绝望!
好的,我已经检查了你的答案,这是原来的
select a.beneficiarioid from
(select beneficiarioid,count(*) total from lmml_ejercicio_2012_3 where programaid=61 group by beneficiarioid order by beneficiarioid) a,
(select beneficiarioid,count(*) total from ejercicio_2012_3 where programaid=61 group by beneficiarioid order by beneficiarioid) where
a.beneficiarioid=b.beneficiarioid and a.total<>b.total;
无论如何,我会尝试你的查询,让你知道!非常感谢你!! 顺便说一句,它是Oracle 11g
答案 0 :(得分:3)
您应该能够使用子查询来获取每个计数,然后使用FULL OUTER JOIN加入它们:
select coalesce(a.progId, b.progId) progid,
coalesce(a.atotal, 0) atotal,
coalesce(b.btotal, 0) btotal
from
(
select progid, count(*) aTotal
from tablea
group by progId
) a
full outer join
(
select progid, count(*) bTotal
from tableb
group by progId
) b
on a.progid = b.progid
where coalesce(a.atotal, 0) <> coalesce(b.btotal, 0);
见SQL Fiddle with Demo。如果一个表中的行在另一个表中不存在,我使用了一个FULL OUTER JOIN。
答案 1 :(得分:1)
即使您的查询在我的数据库上工作正常,我也不愿意设置操作:
(select progid ,count(*) total from tablea group by progid)
minus
(select progid ,count(*) total from tableb group by progid)