【发布时间】:2015-05-26 19:06:46
【问题描述】:
假设我经营一家杂货店并希望了解我的产品的受欢迎程度。
如何从fruit 表中获取每个产品的总计,其中1 表示订单中存在产品,null 表示不存在。
order | apples | oranges | kiwis
================================
1 | 1 | 1 |
2 | | 1 |
3 | | 1 | 1
4 | 1 | | 1
5 | 1 | 1 | 1
我追求的结果是:
apples: 3
oranges: 4
kiwis: 3
到目前为止我已经尝试过:
select count(*) from fruit where apples = '1'
union
select count(*) from fruit where oranges = '1'
union
select count(*) from fruit where kiwis = '1'
这可行,但我不知道哪一行对应于哪个产品。
【问题讨论】:
标签: sql sql-server tsql