我有一張桌子,里面有數十名運動員以及他們每天在健身房度過的時間。我的目標是計算他們一個月的總小時數。他們應該每天都去,如果他們錯過了任何一天(當天為空),我希望該運動員的整個總小時數列顯示為空,而不是他們每月小時數的總和。我有兩列,daily_hours 和 total_hours,定義為 sum(daily_hours)。我的查詢是這樣的,但是它給了我一個語法錯誤
select case
when daily_hours is null then total_hours = null
else total_hours end as name, total_hours
from (select name, sum(daily_hours) as total_hours from athletes group by name);
uj5u.com熱心網友回復:
您有一些問題,您的 case 運算式不正確,并且您缺少派生表的別名 - 但是您可以通過使用 case 運算式和coalesce來確定您的 NULL 值來簡化:
select name,
case when
Min(Coalesce(daily_hours,-1)) > 0 then Sum(daily_hours)
else
null
end as total_hours
from athletes
group by name;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/428857.html
