tags:

views:

29

answers:

1

I would like to combine these 2 statements but can't seem to get it right.

I need 3 total columns. One for total, month and losses.

Here is what I have so far

SELECT Count(patient1.patient_id) AS total FROM patient AS total;
SELECT Count(patient2.patient_id) AS losses, patient2.mo AS `month`
FROM patient AS patient2
WHERE patient1.rx_exp BETWEEN '2010-10-01' AND '2010-11-01';

EDIT I need to have all three columns on a single row.

+1  A: 
SELECT 
    Count(patient_id) AS total,
    Count(case when rx_exp >= '2010-10-01' 
               AND rx_exp < '2010-11-01' then 1 end) AS losses,
    'October' AS `month`
FROM patient;

You could use this for the month but there's probably no point.

MAX(case when rx_exp >= '2010-10-01' 
     AND rx_exp < '2010-11-01' then patient2.mo end) AS `month`
Martin Smith
Your query is giving me the correct totals. Thank you Martin!
jim
Martin, what does the '1' do in the case stmt?
jim
`COUNT` returns the number of `NON NULL` values passed to it. The `CASE` statement will evaluate to `1` if a row is in your target month or `NULL` otherwise. The `1` itself isn't important. You could use `0`, `X`, `patient_id` or any `NON NULL` value and get the same effect.
Martin Smith
Martin, thanks for the explanation and the help. Regarding the month, I'm using other functions to determine the next month and omitted them in my example for brevity.
jim