Selecting COUNT from different criteria on a table

前端 未结 3 400
春和景丽
春和景丽 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:33

    I would use this approach, use COUNT in combination with CASE WHEN.

    SELECT 
        COUNT(CASE WHEN 
            jobs.status_id NOT IN (8,3,11) THEN 1 
        END) as [Count1],
        COUNT(CASE WHEN 
            jobs.due_date < '2011-06-14' 
            AND jobs.status_id NOT IN(8,11,5,3) THEN 1
        END) as [COUNT2],
        COUNT(CASE WHEN
                jobs.due_date BETWEEN '2011-06-14' AND '2011-06-15 06:00:00.000000'
        END) as [COUNT3]
    FROM 
        "jobs"
    WHERE 
         jobs.creator_id = 5 
    

提交回复
热议问题