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