【发布时间】: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