0

I want this table

Booking table

+----+--------+
| ID | Status |
+----+--------+
| 1  | R      |
| 2  | R      |
| 3  | C      |
| 4  | C      |
+----+--------+

To this total number of each

+----------+-----------+
| Reserved | Cancelled |
+----------+-----------+
| 2        | 2         |
+----------+-----------+

So far I got this -

SELECT Status AS Reserves 
FROM Booking WHERE Status = 'R' OR Status = 'C'

Output -

+----------+
| reserves |
+----------+
| R        |
| C        |
| R        |
| C        |
+----------+
toonice
  • 2,211
  • 1
  • 13
  • 20
Nahim
  • 3
  • 2

1 Answers1

2

Put the logic from your WHERE clause into a CASE expression, and sum it over the entire table:

SELECT
    SUM(CASE WHEN Status = 'R' THEN 1 END) AS Reserved,
    SUM(CASE WHEN Status = 'S' THEN 1 END) AS Cancelled
FROM Booking
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360