一个小PIVOT表请帮助

时间:2014-10-02 16:22:24

标签: sql tsql pivot-table

我遇到了一些严重的麻烦,让TSQL中的PIVOT表正常工作。

以下是一些重现源表的示例代码...

Declare @MyAssigneeTable table (
                            VDayID int, 
                            VDate datetime, 
                            StartTime varchar(100), 
                            EndTime varchar(100),
                            PositionID int,
                            RoomID int,
                            RoomDesc varchar(100),
                            Position varchar(100),
                            SortOrder int,
                            Assignee varchar(100)
                        )



INSERT INTO @MyAssigneeTable VALUES (2264, '2013-10-23', '7 AM','12 PM',13,150,'Room 1','Position 1',99,'')
INSERT INTO @MyAssigneeTable VALUES (2264, '2013-10-23', '7 AM','12 PM',14,150,'Room 1','Position 2',99,'Matt')
INSERT INTO @MyAssigneeTable VALUES (2264, '2013-10-23', '7 AM','12 PM',15,150,'Room 1','Position 3',99,'Kevin')
INSERT INTO @MyAssigneeTable VALUES (2265, '2013-10-24', '7 AM','12 PM',13,150,'Room 1','Position 1',99,'')
INSERT INTO @MyAssigneeTable VALUES (2265, '2013-10-24', '7 AM','12 PM',14,150,'Room 1','Position 2',99,'Amber')
INSERT INTO @MyAssigneeTable VALUES (2265, '2013-10-24', '7 AM','12 PM',15,150,'Room 1','Position 3',99,'Lawrence')
INSERT INTO @MyAssigneeTable VALUES (2266, '2013-10-25', '7 AM','12 PM',13,150,'Room 1','Position 1',99,'Chad')
INSERT INTO @MyAssigneeTable VALUES (2266, '2013-10-25', '7 AM','12 PM',14,150,'Room 1','Position 2',99,'Kevin')
INSERT INTO @MyAssigneeTable VALUES (2266, '2013-10-25', '7 AM','12 PM',16,150,'Room 1','Position 3',99,'')

Select * from @MyAssigneeTable

我认为我想做某种PIVOT,但我无法获得所需的输出。在下面所需的表脚本中,我需要从不同的日期驱动列名称,将位置列行作为不同的值,并将与行和列相交的单元格作为受让人。但这对我来说太复杂了。甚至可以这样做吗?这是一个重新创建所需输出的DDL。

Declare @MyDesiredTable table (
                            Position varchar(200), 
                            [2013-10-23] varchar(100), 
                            [2013-10-24] varchar(100), 
                            [2013-10-25] varchar(100)
                         )

INSERT INTO @MyDesiredTable VALUES('Room 1 / Position 1','','','Chad')
INSERT INTO @MyDesiredTable VALUES('Room 1 / Position 2','Matt','Amber','Kevin')
INSERT INTO @MyDesiredTable VALUES('Room 1 / Position 3','Kevin','Lawrence','')

Select * from @MyDesiredTable

任何人都可以帮我吗?

非常感谢你的帮助!!

1 个答案:

答案 0 :(得分:1)

您可以使用动态SQL来构建日期列表(对于标题),并使用MAX来获取特定行的受理人。

只需从SQL Fiddle粘贴原始降价:

查询1

DECLARE @sql AS NVARCHAR(MAX)
DECLARE @cols AS NVARCHAR(MAX)

SELECT @cols= ISNULL(@cols + ',','') + QUOTENAME(CAST(VDate AS date))
FROM (SELECT DISTINCT VDate FROM MyAssigneeTable) AS Dates


SET @sql = N'
  SELECT Position, ' + @cols + '
  FROM (
    SELECT 
      Position = RoomDesc + '' / '' + Position, 
      Vdate = CAST(vdate as date), 
      Assignee 
    FROM MyAssigneeTable
  ) MyAssigneeTable
  PIVOT( MAX( Assignee )
  FOR MyAssigneeTable.vdate IN (' + @cols + ')) AS PivotTable;';

EXEC sp_executesql @sql

<强> Results

|            POSITION | 2013-10-23 | 2013-10-24 | 2013-10-25 |
|---------------------|------------|------------|------------|
| Room 1 / Position 1 |            |            |       Chad |
| Room 1 / Position 2 |       Matt |      Amber |      Kevin |
| Room 1 / Position 3 |      Kevin |   Lawrence |            |

MS SQL Server 2008架构设置

create table MyAssigneeTable 
(
  VDayID int, 
  VDate datetime, 
  StartTime varchar(100), 
  EndTime varchar(100),
  PositionID int,
  RoomID int,
  RoomDesc varchar(100),
  Position varchar(100),
  SortOrder int,
  Assignee varchar(100)
)

INSERT INTO MyAssigneeTable VALUES (2264, '2013-10-23', '7 AM','12 PM',13,150,'Room 1','Position 1',99,'')
INSERT INTO MyAssigneeTable VALUES (2264, '2013-10-23', '7 AM','12 PM',14,150,'Room 1','Position 2',99,'Matt')
INSERT INTO MyAssigneeTable VALUES (2264, '2013-10-23', '7 AM','12 PM',15,150,'Room 1','Position 3',99,'Kevin')
INSERT INTO MyAssigneeTable VALUES (2265, '2013-10-24', '7 AM','12 PM',13,150,'Room 1','Position 1',99,'')
INSERT INTO MyAssigneeTable VALUES (2265, '2013-10-24', '7 AM','12 PM',14,150,'Room 1','Position 2',99,'Amber')
INSERT INTO MyAssigneeTable VALUES (2265, '2013-10-24', '7 AM','12 PM',15,150,'Room 1','Position 3',99,'Lawrence')
INSERT INTO MyAssigneeTable VALUES (2266, '2013-10-25', '7 AM','12 PM',13,150,'Room 1','Position 1',99,'Chad')
INSERT INTO MyAssigneeTable VALUES (2266, '2013-10-25', '7 AM','12 PM',14,150,'Room 1','Position 2',99,'Kevin')
INSERT INTO MyAssigneeTable VALUES (2266, '2013-10-25', '7 AM','12 PM',16,150,'Room 1','Position 3',99,'')