我想要这张桌子
预订表
+----+--------+
| ID | Status |
+----+--------+
| 1 | R |
| 2 | R |
| 3 | C |
| 4 | C |
+----+--------+
每个
的总数+----------+-----------+
| Reserved | Cancelled |
+----------+-----------+
| 2 | 2 |
+----------+-----------+
到目前为止,我得到了这个 -
SELECT Status AS Reserves
FROM Booking WHERE Status = 'R' OR Status = 'C'
输出 -
+----------+
| reserves |
+----------+
| R |
| C |
| R |
| C |
+----------+
答案 0 :(得分:2)
将WHERE
子句中的逻辑放入CASE
表达式中,并将其加到整个表中:
SELECT
SUM(CASE WHEN Status = 'R' THEN 1 END) AS Reserved,
SUM(CASE WHEN Status = 'S' THEN 1 END) AS Cancelled
FROM Booking