1

我的查询有问题,请参阅我有两个表,比如说:

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 在那里)

所以我想在表格中计数不同的地方获得天才,我试过:

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

4

2 回答 2

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。

于 2013-05-31T16:20:17.233 回答
1

即使您的查询在我的数据库上运行良好,我更喜欢设置操作:

(select progid ,count(*) total from tablea group by progid) 
minus
(select progid ,count(*) total from tableb group by progid)  
于 2013-05-31T16:21:14.377 回答