大家好.请问我现有表A,与B.其中A有字段m,n;B有字段j,k
如:
A:
m n
131 q
132 w
135 e
132 e
B:
j k
131 tt
132 mm我现想查出,A中按n分组后.查出m不在B表j中存在的数值.及A表m总数值,请问SQL要怎样写?
结果模拟:select count(distinct m),(select count(distinct m) from a where a.m not in (select j from b )) from a group by n
上面是我想要结果的一个SQL结果模拟,麻烦高手帮忙指点.谢谢
如:
A:
m n
131 q
132 w
135 e
132 e
B:
j k
131 tt
132 mm我现想查出,A中按n分组后.查出m不在B表j中存在的数值.及A表m总数值,请问SQL要怎样写?
结果模拟:select count(distinct m),(select count(distinct m) from a where a.m not in (select j from b )) from a group by n
上面是我想要结果的一个SQL结果模拟,麻烦高手帮忙指点.谢谢
解决方案 »
- oracle dcn不通知是怎么回事,数据是用sqlldr插入的
- 数据库导入导出的问题,请大家指点。。
- 关于提取固定累计数据的语句
- 求:这个语句为什么回报无效列名的错误啊!!!
- 寻找oracle工程师,要绝对高手,现在在广州,解决个问题,有丰厚报酬~
- !!!急!请问是否存在数据不能删除的情况。
- ORACLE8.1.6的问题,大家多帮忙!
- 向各位版主、高手求教Oracle数据导入问题!
- 在oracle8.1.7中如何跟踪数据的sql查询,象sql server 的sql tracer那样的
- 建立的用户有这个错误,在线等待,立即给分
- Over(Partition By ..)??
- 系统重装,oracle在D盘,有没有不需要再安装oracle就能启动的方法。
m n
131 q
132 w
135 e
132 e
B:
j k
131 tt
132 mm
查询出A中按n分组后.查出m不在B表j中存在的数值.及A表m总数值
结果:
q 1 0
w 1 0
e 2 1
create table t1
(
m int,
n varchar2(20))
;
create table t2
(
j int,
k varchar2(10));insert into t1 select 131,'q' from dual
union all
select 132,'w' from dual union all
select 135,'e' from dual union all
select 132,'e' from dual;
commit;insert into t2 select 131,'tt' from dual
union all
select 132,'mm' from dual
commit;
--SQL语句
select k.n,k.cnt1, nvl(k1.cnt2,0) as cnt2
from (select distinct n, count(*) over(partition by n order by n) as cnt1 from t1) k,
(select distinct n, count(*) over(partition by n order by n) as cnt2
from t1
where t1.m not in (select j from t2)) k1
where k.n = k1.n(+)
这个测试通过,可能有优化的方案吧,先这样,在上班呢
Select Distinct n,
Count(m) Over(Partition By n),
(Count(m)
Over(Partition By n) -
(Select Count(m) Over(Partition By n)
From a
Where Not Exists (Select 1 From b Where b.j = a.m)))
From a试试