我的任务是提供每月成功完成的百分比。我看过其他问题中的例子,它们对我没有意义。
我在30天的时间内运行查询以显示完成情况(见下文)。但是,要划分< 10405>的数量。代码按所有困惑的总数计算。
有人可以引导我找到解决方案吗?谢谢。 以下是总完成次数的当前查询:
SELECT groups.description AS Group,notes.date_service, services.code AS Service,
clients.client_id, clients.name_lastfirst_cs AS Client, staff.staff_name_cs AS Staff
from notes, services, clients, staff, groups, address
WHERE notes.zrud_service = services.zzud_service
AND notes.zrud_client = clients.zzud_client
AND notes.zrud_staff = staff.zzud_staff
AND notes.zrud_group = groups.zzud_group
AND services.code IN '10401','10402','10403','10405')
-- 10401 - 403 = successful; 10405 unsuccessful
AND notes.date_service BETWEEN (now() - '30 days'::interval)::timestamp AND now()
答案 0 :(得分:1)
稍微更改了您的查询 - 添加了别名并将连接更改为ANSI样式:
select
g.description as Group,
n.date_service, s.code as Service,
c.client_id,
c.name_lastfirst_cs as Client,
st.staff_name_cs as Staff,
100 * sum(case when s.code in ('10401','10402','10403') then 1 else 0) / count(*) as Succesfull_Percentage
from notes as n
inner join services as s on n.zrud_service = s.zzud_service
inner join clients as c on n.zrud_client = c.zzud_client
inner join staff as st on n.zrud_staff = st.zzud_staff
inner join groups as g on n.zrud_group = g.zzud_group
where
s.code in ('10401','10402','10403','10405') and
n.date_service between (now() - '30 days'::interval)::timestamp and now()