DB - 甲骨文默認行的SQL
create table customer_exercise(
customer_id number,
exercise_id number,
cnt number,
exercise_date date)
數據
1000 10 3 14-AUG-17
1001 20 6 14-AUG-17
1000 20 2 14-AUG-17
是否有可能當記錄不爲條件條款存在獲得默認行?上述查詢 -
1000 20 2
1000 10 3
1001 20 6
由於客戶IDS的條款可能沒有某些運動ID的記錄,是否有可能使用SQL得到像一個結果
select customer_id, exercise_id, sum(cnt)
from customer_exercise
where customer_id in (1000, 1001, 1003)
and exercise_id in (10, 20)
group by customer_id, exercise_id
order by sum(cnt)
結果以下總和爲0爲那些?對於例如1001不具備運動的id = 10的記錄,所以總和爲0。
1001 10 0
1003 10 0
1003 20 0
1000 20 2
1000 10 3
1001 20 6
使用coalesce(sum(cnt),0) –
您是否有其他表格列出所有客戶ID和鍛鍊ID? –
是的,customer_exercise是一個多對多的映射表。有客戶表和鍛鍊表與客戶信息和鍛鍊信息。 – rayne