[英]apply where clause on only one of the selected value
假設我有一個如下表:
id quantity
1 5
2 3
3 6
我想檢索id = 2的行的數量,以及表的總數量,我想我可以做如下的事情:
sql="select quantity where id='2' sum(quantity) as total_quantity from table";
現在,我知道這個說法是錯誤的,但我希望你能理解我在這里要做的事情。 我怎樣才能實現這樣的目標?
使用條件聚合:
select sum(case when id = 2 then quantity end) as qty2,
sum(quantity) as total_quantity
from t;
使用聯合:
SELECT quantity FROM table WHERE id = 2
UNION SELECT SUM(quantity) FROM table
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.