Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL: Multiple count statements with different criteria

Tags:

sql

count

I was just wondering if there is any way to get two separate "count" totals from a table using one query? That is, using a table similar to the following I would like to retrieve each code (distinct) and show the total number of status' NOT equal to X or D, and then have an additional column that shows the total number of status' equal to X or D and the cancel date is greater than a given date (say, the last 14 days).

Table:

Code:  Status  Cancel_Date ----------------------------------- AAA    X       2012-02-01 AAA BBB    X       2012-02-01 AAA    D       2012-01-01 AAA BBB     BBB    D       2012-02-01 BBB    X       2012-01-01 

Example result (based on the above data):

Code:  TotalNotXorD     TotalXorD ------------------------------------ AAA    2                1 BBB    1                2 

TotalNotXorD: e.g.

select code, count(*)  from table  where status not in('X','D')  group by code 

TotalXorD: e.g.

select code, count(*)  from table  where status in('X','D')    and cancel_date >= '2012-02-01'  group by code 

I have looked at doing subqueries etc. but I can't seem to get the results I need.

Any ideas?

Thanks.

like image 431
jj2 Avatar asked Feb 06 '13 23:02

jj2


People also ask

How do I get counts of different values in the same column in SQL?

To count the number of different values that are stored in a given column, you simply need to designate the column you pass in to the COUNT function as DISTINCT . When given a column, COUNT returns the number of values in that column. Combining this with DISTINCT returns only the number of unique (and non-NULL) values.

Can we use COUNT with distinct?

Yes, you can use COUNT() and DISTINCT together to display the count of only distinct rows.

How do you use COUNT AND distinct together?

The correct syntax for using COUNT(DISTINCT) is: SELECT COUNT(DISTINCT Column1) FROM Table; The distinct count will be based off the column in parenthesis. The result set should only be one row, an integer/number of the column you're counting distinct values of.


Video Answer


1 Answers

SELECT  a.code,         COALESCE(b.totalNotXorD, 0 ) totalNotXorD,         COALESCE(c.totalXorD, 0 ) totalXorD, FROM    (SELECT DISTINCT Code FROM tableName) a         LEFT JOIN         (             select code, count(*) totalNotXorD             from table              where status not in('X','D')              group by code         ) b ON a.code = b.code         LEFT JOIN         (             select code, count(*) totalXorD             from table              where status in('X','D')                and cancel_date >= '2012-02-01'              group by code         ) c ON a.code = c.code 

or simply doing CASE

SELECT  Code,         SUM(CASE WHEN status NOT IN ('X','D') OR status IS NULL THEN 1 ELSE 0 END) TotalNotXorD,         SUM(CASE WHEN status IN ('X','D') AND cancel_date >= '2012-02-01' THEN 1 ELSE 0 END) TotalXorD   FROM    tableName GROUP   BY Code 
like image 99
John Woo Avatar answered Sep 18 '22 20:09

John Woo