Skip to main content
added 84 characters in body
Source Link
Akina
  • 20.3k
  • 2
  • 19
  • 22

Trying to find 3 separate counts within one column of a table, each with separate WHERE clauses. I've got 2 tables in the query, so one join. Thought this looked correct but it will not run:

SELECT stores.caption,

COUNT ( WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30' AND tasks.deleted_at IS NULL) AS TaskTotal ,

COUNT ( WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30' AND tasks.deleted_at IS NULL AND tasks.completed_at IS NULL) AS TaskOutstanding,

COUNT ( WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30'
AND tasks.deleted_at IS NULL AND tasks.completed_at IS NULL AND tasks.deadline < '2019-05-16') AS TaskOverdue

SELECT stores.caption,
     
COUNT (
            WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30' 
            AND tasks.deleted_at IS NULL) 
            AS TaskTotal , 
    
 COUNT (
            WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30' 
            AND tasks.deleted_at IS NULL 
            AND tasks.completed_at IS NULL) AS TaskOutstanding, 
     
COUNT (
            WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30'     
            AND tasks.deleted_at IS NULL 
            AND tasks.completed_at IS NULL 
            AND tasks.deadline < '2019-05-16') AS TaskOverdue

    FROM tasks
    FULL JOIN stores ON tasks.store_id=stores.id
    GROUP BY stores.caption;

Trying to find 3 separate counts within one column of a table, each with separate WHERE clauses. I've got 2 tables in the query, so one join. Thought this looked correct but it will not run:

SELECT stores.caption,

COUNT ( WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30' AND tasks.deleted_at IS NULL) AS TaskTotal ,

COUNT ( WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30' AND tasks.deleted_at IS NULL AND tasks.completed_at IS NULL) AS TaskOutstanding,

COUNT ( WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30'
AND tasks.deleted_at IS NULL AND tasks.completed_at IS NULL AND tasks.deadline < '2019-05-16') AS TaskOverdue

FROM tasks
FULL JOIN stores ON tasks.store_id=stores.id
GROUP BY stores.caption;

Trying to find 3 separate counts within one column of a table, each with separate WHERE clauses. I've got 2 tables in the query, so one join. Thought this looked correct but it will not run:

SELECT stores.caption,
     
COUNT (
            WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30' 
            AND tasks.deleted_at IS NULL) 
            AS TaskTotal , 
    
 COUNT (
            WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30' 
            AND tasks.deleted_at IS NULL 
            AND tasks.completed_at IS NULL) AS TaskOutstanding, 
     
COUNT (
            WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30'     
            AND tasks.deleted_at IS NULL 
            AND tasks.completed_at IS NULL 
            AND tasks.deadline < '2019-05-16') AS TaskOverdue

    FROM tasks
    FULL JOIN stores ON tasks.store_id=stores.id
    GROUP BY stores.caption;
Source Link
mtug94
  • 1
  • 1
  • 1

SQL: How to ask for multiple counts from the same table within a join

Trying to find 3 separate counts within one column of a table, each with separate WHERE clauses. I've got 2 tables in the query, so one join. Thought this looked correct but it will not run:

SELECT stores.caption,

COUNT ( WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30' AND tasks.deleted_at IS NULL) AS TaskTotal ,

COUNT ( WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30' AND tasks.deleted_at IS NULL AND tasks.completed_at IS NULL) AS TaskOutstanding,

COUNT ( WHEN tasks.created_at BETWEEN '2019-04-01' AND '2019-04-30'
AND tasks.deleted_at IS NULL AND tasks.completed_at IS NULL AND tasks.deadline < '2019-05-16') AS TaskOverdue

FROM tasks
FULL JOIN stores ON tasks.store_id=stores.id
GROUP BY stores.caption;