Had one query where we have to give count of Surgeries done in the order of Speciality (Department), OT type, OT Category, OT Sub-Category. We wrote a simple SQL Query with Group by clause. So we were getting records like: (Example)
Speciality Type Category Sub-category count
CTVS Closed Heart Congenital Interrupted Aortic Arch 456
CTVS Closed Heart Congenital Ventricular Septal Defect 678
CTVS Closed Heart Ischemic Interrupted Aortic Arch 123
CTVS Closed Heart Ischemic Ventricular Septal Defect 465
CTVS Open Heart Congenital Interrupted Aortic Arch 138
CTVS Open Heart Congenital Ventricular Septal Defect 615
CTVS Open Heart Ischemic Interrupted Aortic Arch 513
CTVS Open Heart Ischemic Ventricular Septal Defect 908
So, how to get Counts at all the three levels: Type, Category and Sub-category?
The trick was in use of CUBE
Syntax: group by cube(column1, column2, column3,...,columnn)
and then it gave the output, the way we wanted
Speciality Type Category Sub-category count
CTVS Closed Heart Congenital Interrupted Aortic Arch 456
CTVS Closed Heart Congenital Ventricular Septal Defect 678
CTVS Closed Heart Congenital................................................... 1134
CTVS Closed Heart Ischemic Interrupted Aortic Arch 123
CTVS Closed Heart Ischemic Ventricular Septal Defect 465
CTVS Closed Heart Ischemic...................................................... 588
CTVS Closed Heart......................................................................... 1722
CTVS Open Heart Congenital Interrupted Aortic Arch 138
CTVS Open Heart Congenital Ventricular Septal Defect 615
CTVS Open Heart Congenital.................................................... 753
CTVS Open Heart Ischemic Interrupted Aortic Arch 513
CTVS Open Heart Ischemic Ventricular Septal Defect 908
CTVS Open Heart Ischemic....................................................... 1421
CTVS Open Heart ........................................................................... 2174
Cool...So, now no more struggling with multiple totals :)
preventing Moodle time-outs
5 days ago