|
|
Question : Rollup/cube query Oracle 10g/9i. One final help needed......Plzzzzzzzzz help this poor soul
|
|
As Sujith and Paquicuba have been answering my questions, I needed to modify the query to suit my own one. However when I did so, I didn't get the desired results. The reason was the structure of the query I needed was something different. Here's a model of what I want in my query. Taking the same example of student marks.
ID SEMESTER SUBJECT_TYPE SUBJECT THEORY_TOT THEORY_OBT PRAC_TOT PRAC_OBT MARKS 9 4 MAIN Maths 50 42 50 39 81 9 4 SECONDARY English 50 40 40 9 4 SECONDARY Computers 50 48 50 45 93 10 5 MAIN Maths 50 45 50 40 85 10 5 SECONDARY History 50 43 43 11 4 MAIN Maths 50 42 50 39 81 11 4 SECONDARY English 50 40 40 11 4 SECONDARY Computers 50 48 50 45 93
That's the sample table(please forgive the formatting. I've done it properly. Don't know why this textbox doesn't take it as it is). And This is the desired output ID SEM SUBJECT_TYPE SUBJECT THEORY_TOT THEORY_OBT PRAC_TOT PRAC_OBT MARKS 9 4 MAIN Maths 50 42 50 39 81 Total 81 9 4 SECONDARY English 50 40 40 9 4 SECONDARY Computers 50 48 50 45 93 Total 133 10 5 MAIN Maths 50 45 50 40 85 Total 85 10 5 SECONDARY History 50 43 43 Total 43 11 4 MAIN Maths 50 42 50 39 81 Total 81 11 4 SECONDARY English 50 40 40 11 4 SECONDARY Computers 50 48 50 45 93 Total 133
And then a rank Column to be added with the person with rank 1 at the top like this....
FINAL OUTPUT: ID SEM SUBJECT_TYPE SUBJECT THEORY_TOT THEORY_OBT PRAC_TOT PRAC_OBT MARKS RANK 9 4 SECONDARY English 50 40 40 9 4 SECONDARY Computers 50 48 50 45 93 Total 133 1 11 4 SECONDARY English 50 40 40 11 4 SECONDARY Computers 50 48 50 45 93 Total 133 1 10 5 MAIN Maths 50 45 50 40 85 Total 85 2
9 4 MAIN Maths 50 42 50 39 81 Total 81 3 11 4 MAIN Maths 50 42 50 39 81 Total 81 3 10 5 SECONDARY History 50 43 43 Total 43 4
Please don't go about the logics about the model I've posted. The result that I've wanted for the past 1 week is somewhat like this. Can anybody please help me out. I would be very highly grateful to u guys @experts exchange. I would gladly give a 1000points if its available. Really this question is getting to me now guys.. Plz help....
|
Answer : Rollup/cube query Oracle 10g/9i. One final help needed......Plzzzzzzzzz help this poor soul
|
|
Here you go. The only difference between your excel example and the output from this is that the subject column is alphabetically ordered where it is not the total row.
select case when rk is not null or subject_rk > 1 then null else studid end studid, case when rk is not null or subject_rk > 1 then null else semester end semester, case when rk is not null or subject_rk > 1 then null else subject_type end subject_type, subject, case when rk is not null or theory_tot = 0 then null else theory_tot end theory_tot, case when rk is not null or theory_obt = 0 then null else theory_obt end theory_obt, case when rk is not null or pract_tot = 0 then null else pract_tot end pract_tot, case when rk is not null or pract_tot = 0 then null else pract_obt end pract_obt, marks, rk from ( select studid, semester, subject_type, subject, theory_tot, theory_obt, pract_tot, pract_obt, marks, subject_rk, rk from ( select studid, semester, subject_type, case when subject is null then 'Total' else subject end subject, theory_tot, theory_obt, pract_tot, pract_obt, marks, subject_rk, case when subject is null then dense_rank() over (partition by null order by case when subject is null then marks else 0 end desc) else null end rk from ( select studid, semester, subject_type, subject, sum(theory_tot) theory_tot, sum(theory_obt) theory_obt, sum(pract_tot) pract_tot, sum(pract_obt) pract_obt, sum(marks) marks, rank() over (partition by studid, semester, subject_type order by subject) subject_rk from TMP_SP_MARKS group by grouping sets ((studid, semester, subject_type, subject),(studid, semester, subject_type)) ) ) order by max(rk) over (partition by studid, semester, subject_type), studid, semester, subject_type, nvl(rk, 0) )
|
|
|
|
|