作者adrianshum (Alien)
看板Database
标题Re: [SQL ] 如何同时使用2个count找不同的解?
时间Tue Oct 27 02:02:25 2009
※ 引述《yuchia69 (最爱 梁静茹 ^^)》之铭言:
: 各位大大你好 我用SQL2000 SP4
: (在DB上 我的权限只能query)
: 我有2个table a(课码a1,课名a2)
: b(课码b1,学号b2,分数b3)
: 我要如何同时算出 修课人数 和 及格人数
: 我的写法是
: select a.a2, count(*) as '修课人数', count(*) as '及格人数'
: from a,b
: where a.a1=b.b1
: group by a.a1
: order by a.a1
: 可是b.b3>=60 不知道要写在哪边 才能算出及格人数
b join 两次吧
大概是 select a, count(bAll.id), count(bPass.id)
from a
inner join b bAll
where bAll.b1 = a.a1
left join b bPass
where bPass.id = bAll.id
and bPass.b3 >= 60
group by a.a1
也可以用 case 之类的做法, 留给你自己想吧
(我不熟 sql server, 回文只是根据一般的 dbms/sql)
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 203.218.221.177
※ 编辑: adrianshum 来自: 203.218.221.177 (10/27 02:03)
※ 编辑: adrianshum 来自: 203.218.221.177 (10/27 02:06)