SQL数据透视表分组 [英] SQL Pivot Table Grouping

查看:93
本文介绍了SQL数据透视表分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张桌子,如下:

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

我想在SQL中将其透视为:

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.

推荐答案

如果不需要通过动态操作,则应该执行简单的条件聚合.

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

Select Date
      ,Ticket
      ,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

这篇关于SQL数据透视表分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆