Selecting COUNT from different criteria on a table

前端 未结 3 439
春和景丽
春和景丽 2021-02-04 05:25

I have a table named \'jobs\'. For a particular user a job can be active, archived, overdue, pending, or closed. Right now every page request is generating 5 COUNT queries and

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 05:52

    This is the typical solution. Use a case statement to break out the different conditions. If a record meets it gets a 1 else a 0. Then do a SUM on the values

      SELECT
        SUM(active_count) active_count,
        SUM(overdue_count) overdue_count
        SUM(due_today_count) due_today_count
      FROM 
      (
    
      SELECT 
        CASE WHEN jobs.status_id NOT IN (8,3,11) THEN 1 ELSE 0 END active_count,
        CASE WHEN jobs.due_date < '2011-06-14' AND jobs.status_id NOT IN(8,11,5,3)  THEN 1 ELSE 0 END  overdue_count,
        CASE WHEN jobs.due_date BETWEEN '2011-06-14' AND '2011-06-15 06:00:00.000000' THEN 1 ELSE 0 END  due_today_count
    
        FROM "jobs"
        WHERE
          jobs.creator_id = 5 ) t
    

    UPDATE As noted when 0 records are returned as t this result in as single result of Nulls in all the values. You have three options

    1) Add A Having clause so that you have No records returned rather than result of all NULLS

       HAVING SUM(active_count) is not null
    

    2) If you want all zeros returned than you could add coalesce to all your sums

    For example

     SELECT
          COALESCE(SUM(active_count)) active_count,
           COALESCE(SUM(overdue_count)) overdue_count
          COALESCE(SUM(due_today_count)) due_today_count
    

    3) Take advantage of the fact that COUNT(NULL) = 0 as sbarro's demonstrated. You should note that the not-null value could be anything it doesn't have to be a 1

    for example

     SELECT
          COUNT(CASE WHEN 
                jobs.status_id NOT IN (8,3,11) THEN 'Manticores Rock' ELSE NULL
           END) as [active_count]
    

提交回复
热议问题