0

我的任务是提供每月成功完成的百分比。我已经查看了其他问题中的示例,它们对我来说没有意义。

我运行查询以显示 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()
4

1 回答 1

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()
于 2013-09-07T22:16:06.293 回答