oracle - Sql to order results by sum -
db used - oracle
create table customer_exercise( customer_id number, exercise_id number, cnt number, exercise_date date)
table data
1000 10 3 17-dec-15 1001 20 6 19-dec-15 1000 20 2 20-dec-15 1003 20 9 20-dec-15 1000 20 6 22-dec-15 1000 30 10 23-dec-15 1001 10 25 10-dec-15
is possible results using sql such sum(cnt) exercise_id 20 appears first in resultset?
select customer_id , exercise_id, sum(cnt) customer_exercise customer_id in (1000, 1001, 1003) , exercise_id in (20) group customer_id, exercise_id order sum(cnt) 1001 20 6 1000 20 8 1003 20 9 select customer_id , exercise_id, sum(cnt) customer_exercise customer_id in (1000, 1001, 1003) , exercise_id not in (20) group customer_id, exercise_id order sum(cnt) 1000 10 3 1000 30 10 1001 10 25
what trying merge results of above 2 queries 1 sql. possible write single sql fetch resultset below?
1001 20 6 1000 20 8 1003 20 9 1000 10 3 1000 30 10 1001 10 25
i think be:
select customer_id , exercise_id, sum(cnt) customer_exercise customer_id in (1000, 1001, 1003) group customer_id, exercise_id order (case when exercise_id in (20) 1 else 2 end), sum(cnt)
Comments
Post a Comment