请帮我解决这个问题。我想找到员工数量的员工人数。从员工表中的T和N开始,按表分组。查询如下:
select
m.institution_dist,
count(e.emp_pf_acc_number like 'T%') as tcnt,
count(e.emp_pf_acc_number like 'N%') as ncnt
from
pf_emp e
left join
pfmast_institution m on e.emp_workplace_code = m.institution_code
where
e.retired <> 'Y'
and emp_classification_code = '3'
and m.institution_dist in (
select dist_name
from pfmast_dist
)
group by m.institution_dist
问题在于,我得到的结果与从T和N开始的员工相同。
答案 0 :(得分:2)
只需将or null
添加到计数表达式
count(e.emp_pf_acc_number like 'T%' or null) as tcnt,
count(e.emp_pf_acc_number like 'N%' or null) as ncnt
count
计数不为空。当您的原始表达式返回true
或false
时,它将始终被计算在内。 false or null
评估为null
,因此不计算在内。
where
子句
and exists (
select 1
from pfmast_dist
where m.institution_dist = dist_name
)
答案 1 :(得分:1)
尝试此查询:
Select * from
(select m.institution_dist,
Count(e.emp_pf_acc_number like 'T%') as tcnt
from pf_emp e
left join
pfmast_institution m on e.emp_workplace_code = m.institution_code
where e.retired <> 'Y'
and emp_classification_code = '3'
and m.institution_dist in ( select dist_name from pfmast_dist)
group by m.institution_dist ) x
full outer join
(select b.institution_dist,count(a.emp_pf_acc_number like 'N%') as ncnt
from pf_emp a
left join
pfmast_institution b on a.emp_workplace_code = b.institution_code
where b.retired <> 'Y'
and emp_classification_code = '3'
and b.institution_dist in ( select dist_name from pfmast_dist )
group by b.institution_dist) y
on x.institution_dist = y.institution_dist