【问题标题】:Creating a pivot table in T-SQL?在 T-SQL 中创建数据透视表?
【发布时间】:2012-12-28 17:35:04
【问题描述】:

我真的需要一些帮助来创建数据透视表。我在某些行中有数据,而这些数据需要出现在列中,并列在其他记录中的值旁边。 目前数据格式如下:

Region  |  Location  |  Customer | CustomerKey |Status
North   |  New York  |  John     | 111         |Active
North   |  New York  |  Mary     | 112         |Active
North   |  Delaware  |  Bob      | 113         |Idle
North   |  New Jersey|  Bob      | 113         |Active
West    |  California|  Bob      | 113         |Inactive
West    |  Washington|  Greg     | 114         |Inactive
West    |  Utah      |  Tim      | 115         |Active
North   | All States |  Bob      | 113         |VIP Customer
North   | All States |  Mary     | 112         |Regular Customer
West    | All States |  Bob      | 113         |Regular Customer
West    | All States |  Tim      | 115         |Regular Customer
West    | All States |  Greg     | 114         |VIP Customer
North   | All States |  John     | 111         |Regular Customer

问题在于“状态”列,它可以有一组值(非活动/活动/空闲)和另一组值(VIP 客户和普通客户)。当“位置”列是“所有州”时,它使用 VIP/常规值。我想添加一列,以使数据沿以下行显示:

Region  |  Location  |  Customer | CustomerKey |Status   | VIPStatus
North   |  New York  |  John     | 111         |Active   | No
North   |  New York  |  Mary     | 112         |Active   | No
North   |  Delaware  |  Bob      | 113         |Idle     | Yes
North   |  New Jersey|  Bob      | 113         |Active   | Yes
West    |  California|  Bob      | 113         |Inactive | No
West    |  Washington|  Greg     | 114         |Inactive | Yes
West    |  Utah      |  Tim      | 115         |Active   | No

基本上,如果客户有一个状态为“VIP 客户”的记录,在一个区域和相应的位置值“所有州”的组合下,那么它将显示一个“VIPStatus”为“是”或在该给定区域下该客户的任何记录下为“否”(无论位置状态如何)。有一个简单的解决方案吗?对于在 T-SQL 中重新排列这些数据的任何帮助,我们将不胜感激。

【问题讨论】:

    标签: sql tsql pivot pivot-table


    【解决方案1】:

    您应该能够多次加入表以获得所需的结果:

    select t1.region,
      t1.location,
      t1.customer,
      t1.customerkey,
      t1.status,
      case when t2.status is not null then 'Yes' else 'No' end VIPStatus
    from yourtable t1
    left join yourtable t2
      on t1.CustomerKey = t2.CustomerKey 
      and t2.Location = 'All States' 
      and t2.status = 'VIP Customer'
    where t1.Location <> 'All States' 
    

    SQL Fiddle with Demo

    结果是:

    | REGION |   LOCATION | CUSTOMER | CUSTOMERKEY |   STATUS | VIPSTATUS |
    -----------------------------------------------------------------------
    |  North |   New York |     John |         111 |   Active |        No |
    |  North |   New York |     Mary |         112 |   Active |        No |
    |  North |   Delaware |      Bob |         113 |     Idle |       Yes |
    |  North | New Jersey |      Bob |         113 |   Active |       Yes |
    |   West | California |      Bob |         113 | Inactive |       Yes |
    |   West | Washington |     Greg |         114 | Inactive |       Yes |
    |   West |       Utah |      Tim |         115 |   Active |        No |
    

    【讨论】:

    • 谢谢,这样做了,尽管我必须在 JOIN 中添加 'AND t1.Region = t2.Region'。非常感谢。
    猜你喜欢
    • 2020-08-18
    • 2022-01-03
    • 2014-08-07
    • 2021-01-19
    • 2011-11-06
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    相关资源
    最近更新 更多