我该怎么做才是这样:
|type|quantity|
+----+--------+
|shoe| 10 |
|hat | 2 |
|shoe| 7 |
|shoe| 1 |
|hat | 5 |
得到这个:
|shoes|hats|
+-----+----+
| 18 | 7 |
我该怎么做?到目前为止,我还没有提出一个有效的查询,我认为应该看起来像这样:
SELECT
SUM(CASE type WHEN 'shoe' then quantity ELSE 0 END) AS "shoes",
SUM(CASE type WHEN 'hat' then quantity ELSE 0 END) AS "hats"
FROM items
GROUP BY type
答案 0 :(得分:1)
只需删除group by
即可。您只想要一行:
SELECT
SUM(CASE type WHEN 'shoe' then quantity ELSE 0 END) AS "shoes",
SUM(CASE type WHEN 'hat' then quantity ELSE 0 END) AS "hats"
FROM items ;