Question : MySQL query, multiple tables and ordered/grouped results

Hi All,

I'm not sure how I can explain this but I'll give it a go.

I'm trying to extract the the number of bookings grouped by booking_type together with the number of cancelled and rejected bookings in one statement.

For example;

General      20
Loan          14
Selfship      12
Cancelled    5
Rejected     3

I can get the booking type easily enough by doing the following;
select b.booking_type, count(*)
from BOOKING b, ADMIN a
where b.booking_id = a.booking_id
and a.approved = "Approved"
group by booking_type

However, I can't seem to be able to include the count of the approved status unless I do the query for each approved status manually in the `and a.approved = "Cancelled"`.

Is there anyway of including these results in the main select?
Related Solutions: MySQL sum() case/if

Answer : MySQL query, multiple tables and ordered/grouped results

you mean, something like this:
1:
2:
3:
4:
5:
6:
7:
8:
select b.booking_type
, sum(case when a.approved = 'Approved' then 1 else 0 end) approved
, sum(case when a.approved = 'Cancelled' then 1 else 0 end) cancelled
, count(*) total
from BOOKING b, ADMIN a
where b.booking_id = a.booking_id
group by booking_type
Random Solutions  
 
programming4us programming4us