[英]SQL get sum of one column and sum of different column with where in one query?
這是我的桌子
| value | count | type |
| 5 | 2 | Bike |
| 5 | 2 | Bike |
| 5 | 1 | Car |
| 2 | 3 | Car |
我正在嘗試獲取value = 5的count列的總和。
然后在同一查詢中獲得count列的總和,其中value = 5並鍵入= Bike。
因此結果應為:
第一筆款項為5
第二個總和為4
一個查詢有可能嗎?
您可以使用條件聚合在一個查詢中完成此操作。
select
sum(case when value=5 then count else 0 end) as sum_1
,sum(case when value=5 and type='Bike' then count else 0 end) as sum_2
from yourtable
對於mysql
select sum(count) from table where value = 5;
select sum(count) from table where value = 5 and Type like 'bike';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.