Tuesday, August 5, 2008

SQL: GROUP BY CUBE(column1, column2,..., column n)

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 :)



SQL: GROUP BY CUBE(column1, column2,..., column n)SocialTwist Tell-a-Friend
Bookmark and Share

3 comments:

  1. I am sure you would have noticed that the subtotal rows have a 'null' value at the level in which subtotalling. You can make this a little more readable to the user by using the IsNull Function [ IsNull(Column_Name, Value_If_Null] and displaying a word like say, 'Subtotal', instead of a boring blank null. :)

    ReplyDelete
  2. Yes Naams,
    I did observe that...will try out the option suggested and come back with an update into post

    R

    ReplyDelete
  3. Thought i might add that Excel could do such a small thing...not trying to milk the brains of Software guys (they are much in demand and are needed elsewhere). This is a short guided tour by Microsoft in creating Pivot tables and consolidating the data. See if it is useful!
    http://office.microsoft.com/training/training.aspx?AssetID=RC102058721033&CTT=6&Origin=RP102058711033

    ReplyDelete

Blog Widget by LinkWithin
 
Clicky Web Analytics