哪位大侠 帮忙解答一下sql问题

2024-12-02 20:53:50
推荐回答(2个)
回答1:

一个嵌套?

select t.zcs,t.lzcs,trunc(t.zcs/t.lzcs) as w,t.staff_id,t.staff_id
from
(SELECT SUM(CASE WHEN a.serial_id IS NOT NULL THEN 1 ELSE 0 END) zcs,
SUM(CASE WHEN b.serial_id IS NOT NULL THEN 1 ELSE 0 END) lzcs,
a.staff_id,
b.staff_id 
FROM scb_tmp_cy a,scb_tmp_jscs b 
WHERE a.staff_id=b.staff_id 
AND a.serial_id=b.serial_id
GROUP BY a.staff_id,b.staff_id) t

回答2:

没必要用case语句,count会过滤空值
select

count(a.serial_id) zcs,
count(b.serial_id) lzcs,
trun(count(a.serial_id)/count(b.serial_id)) as w,
a.staff_id,
b.staff_id
FROM
scb_tmp_cy a,scb_tmp_jscs b
WHERE
a.staff_id=b.staff_id AND a.serial_id=b.serial_id
GROUP BY
a.staff_id,
b.staff_id;