[SQL ] 寻找特定字段为Null之组别

楼主: adoniscomes (adonis)   2018-05-16 15:58:49
数据库名称: Oracle + PL-SQL
内容/问题描述:
学生Table stb 组别对应Table sTeam
学生编号 毕业年月 组别 学生编号
sno ym tno sno
01 10704 1 01
02 1 02
03 10703 1 03
04 10701 2 04
05 10701 2 05
06 10701 2 06
07 3 07
08 3 08
09 3 09
10 10701 4 10
11 10702 4 11
12 10703 4 12
想要列出 ‘每个ym字段均不为null’之组别:
tno
2
4
目前想法写法是
Select s2.tno
From stb s1, sTeam s2
Where s1.sno = s2.sno
And s1.ym is null
Group by s2.tno
Having count(s.sno) = 0
但出来的结果似乎不太对...
有想到Not exist的方向但因为不熟所以不知道从何下手..
来这边请教众大神指导帮忙 感谢!
作者: adrianshum (Alien)   2018-05-17 20:02:00
Select distinct t.tno from steam t where not exist(select 1 from steam t1 inner join stb s1 on s1.sno=t1.sno where t1.tno =t.tno and s1.ymd is null)

Links booklink

Contact Us: admin [ a t ] ucptt.com