sql order by with group by rollup

こ雲淡風輕ζ 提交于 2021-02-11 12:57:15

问题


My query looks like below with the foll. output

Current Output

Role            Cases prepped % Completed    
State Member 1  10    5       50%
State Member 2  10    7       70%
State President 10    2       20%
Summary         30    14      46.6%

Output Expected

Role            Cases prepped % Completed
State President 10    2       20%
State Member 1  10    5       50%
State Member 2  10    7       70%
Summary         30    14      46%

Roles table

id name
30 State President
40 State Member

This is my query,

SELECT COALESCE(ROLE, 'Summary') ROLE,   
       count(*) AS cases, 
       SUM(CASE WHEN PREPARED = 'Y' THEN 1
                ELSE 0
           END) AS prepped, 
 round(avg(case when prepared = 'Y' then 100 else 0 end),2)||'%' as % Completed
FROM
  (SELECT CASE
              WHEN r.id = 30 THEN r.name
              ELSE r.name || ' ' || u.case_member_id
          END AS ROLE,
          bi.prepared
   FROM cases c
   LEFT JOIN case_inventory ci ON ci.case_id = c.id
   AND c.id = ci.case_id
   AND c.delete_date IS NULL
   AND ci.case_id =40
  Left JOIN users u ON ci.assigned_to = u.id
   Left JOIN ROLES r ON u.role_id = r.id
   Left JOIN user_cases_map uc ON c.id = uc.case_id
   AND uc.id = 1572919346)
GROUP BY ROLLUP (ROLE);

I now want to order the rows with respect to the role. The 1st record should be the State president and then followed by state memebr 1. state member 2. and so on. I tried to have an order by in the inner clause but it did not help. It doesnt have any effect. Adding in the outer select also doesnt change anything. Any help highly appreciated. Thank you.


回答1:


Starting from your current query, you can just use avg():

SELECT 
    COALESCE(ROLE, 'Summary') ROLE,   
    COUNT(*) AS cases, 
    SUM(CASE WHEN PREPARED = 'Y' THEN 1 ELSE 0 END) AS prepped, 
    AVG(CASE WHEN PREPARED = 'Y' THEN 1 ELSE 0 END) AS ratio_completed
FROM ...

This returns a numeric between 0 and 1. You can replace THEN 1 with THEN 100 if you prefer a percentage (between 0 and 100).




回答2:


It is a good practice in SQL to split a more complex logic in several subqueries.

I'll recomend to use one subquery (basically your query) to calculate the counts and an other subquery to calculate the ratio.

The new main query would be

select ROLE, CASES, PREPPED,
(PREPPED/nullif(CASES,0))* 100 proc_completed
from (your query);

ROLE                 CASES    PREPPED PROC_COMPLETED
--------------- ---------- ---------- --------------
State President         10          2             20
State Member 1          10          5             50
State Member 2          10          7             70
State Member 2           0          0               
Summary                 10         14            140

Note that I added a new line with the edge case of count = 0 and I use the nullif function to avoid problems.




回答3:


Use just count(*) where you currently have sum(count(*)) in your expression for comp.



来源:https://stackoverflow.com/questions/64610906/sql-order-by-with-group-by-rollup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!