我有2张桌子。我有一个sql,可以显示时间,日期,employee_id,并计算重复ID。现在我想允许系统可以统计所有以字母开头的名字' L'并显示每天的所有员工名字的总和以字母' L& #39;
select distinct a.employee_id,b.name,a.type,a.date,a.time,count(*)
from deal_records a, staff_table b
where a.employee_id = b.employee_id && a.date>='2017-1-1'
and a.date <'2017-2-1'&& b.name like "L%"
group by a.employee_id;
答案 0 :(得分:0)
在单个查询中同时包含select distinct
和group by
的气味很多,请适当地扩展group by
子句。
关于回答问题,请将count(*)
替换为sum(case when b.name like 'L%' then 1 end)
。
答案 1 :(得分:0)
select a.employee_id,b.name,a.type,a.date,a.time,
sum( case when b.name like "L%" 1 else 0 end) as all_count
from deal_records a, staff_table b
where a.employee_id = b.employee_id
&& trunc(a.date) between to_date('01.01.2017', 'DD.MM.YYYY') and to_date('01.02.2017', 'DD.MM.YYYY')
group by a.employee_id
order by trunc(a.date)