Aswin Kannan Aswin Kannan - 1 year ago 64
SQL Question

SQL Pivot Table Grouping

I have a table as follows:

Date Ticket Question Response
2016-10-01 1 Score? 10
2016-10-01 1 Reason? Awesome
2016-10-02 2 Score? 9
2016-10-02 2 Reason? Good
2016-10-03 3 Score? 8
2016-10-03 3 Reason? Okay

I want to Pivot it in SQL as:

Date Ticket Score? Reason?
2016-10-01 1 10 Awesome
2016-10-02 2 9 Good
2016-10-03 3 8 Okay

Can someone please help? I'm happy to provide more details if required.

enter image description here

Answer Source

If it does not need to by dynamic, a simple conditional aggregation should do.

Select Date
      ,Score  = max(case when Question='Score?'  then Response else null end)
      ,Reason = max(case when Question='Reason?' then Response else null end)
 From YourTable
 Group By Date,Ticket
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download