我正在研究一种类似于下面的总线路由系统的运输类型的路由系统。 我有一个视图,它给我这样的输出。我需要在时间表上进行调整,其中时间表的数量可以是可变数量。
我的查询应该会在图片中显示下面给出的输出。我尝试使用Case Statements但是我遇到了返回行数的问题。
http://i.stack.imgur.com/kumTw.jpg ![在此输入图像说明] [1]
以下是生成表格和参考数据的脚本:
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ScheduleTest]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[ScheduleTest](
[StationName] [nvarchar](255) NULL,
[ScheduleNumber] [nvarchar](255) NULL,
[ArrivalTime] [nvarchar](20) NULL,
[DepartureTime] [nvarchar](20) NULL
) ON [PRIMARY]
END
GO
--Insert Scripts For Schedule A
Insert into ScheduleTest Values ('Chicago, IL, (Union Station)', 'ScheduleA', NULL, '02:45')
Insert into ScheduleTest Values ('Chicago, IL, (DownTown)', 'ScheduleA', '02:55', '03:00')
Insert into ScheduleTest Values ('Benton, MI, Harbor', 'ScheduleA', '08:00', NULL) --Benton in this case
--is final destination so departure time is null
-- Insert Scripts for Schedule B (Another Which runs in the morning)
Insert into ScheduleTest Values ('Chicago, IL, (Union Station)', 'ScheduleB', NULL, '06:00')
Insert into ScheduleTest Values ('Chicago, IL, (DownTown)', 'ScheduleB', '06:10', '06:15')
Insert into ScheduleTest Values ('Benton, IL, Harbor', 'ScheduleB', '11:00', NULL)
我认为我不能在Sql server 2005中使用透视,因为它需要某种聚合来处理。我这里没有任何聚合。
答案 0 :(得分:0)
我不确定这是否非常有效,但您可以尝试一下:
;WITH CTE AS
(
SELECT StationName, ScheduleNumber, ArrivalTime ScheduleTime, 'Arrival' [Arrival/Departure]
FROM ScheduleTest
WHERE ArrivalTime IS NOT NULL
UNION ALL
SELECT StationName, ScheduleNumber, DepartureTime, 'Departure'
FROM ScheduleTest
WHERE DepartureTime IS NOT NULL
)
SELECT StationName,
MIN(CASE WHEN ScheduleNumber = 'ScheduleA' THEN ScheduleTime ELSE NULL END) ScheduleA,
MIN(CASE WHEN ScheduleNumber = 'ScheduleB' THEN ScheduleTime ELSE NULL END) ScheduleB,
[Arrival/Departure]
FROM CTE
GROUP BY StationName, [Arrival/Departure]
答案 1 :(得分:0)
我是从不同的来源获得的...这是有效的,并且感谢建议这个答案的人
如果OBJECT_ID('tempdb ..#ScheduleTest')IS NOT NULL DROP TABLE #ScheduleTest GO
如果OBJECT_ID('tempdb ..#tmp')IS NOT NULL DROP TABLE #tmp GO
CREATE TABLE #ScheduleTest( [StationName] nvarchar NULL, [ScheduleNumber] nvarchar NULL, [ArrivalTime] nvarchar NULL, [DepartureTime] nvarchar NULL )[主要]
GO
- 为附表A插入脚本 插入#ScheduleTest值('Chicago,IL,(Union Station)','ScheduleA',NULL,'02:45') 插入#ScheduleTest值('Chicago,IL,(DownTown)','ScheduleA','02:55','03:00') 插入#ScheduleTest值('Benton,IL,Harbour','ScheduleA','08:00',NULL) - 在这种情况下的实况 - 是最终目的地,因此出发时间为空
- 为附表B插入脚本(另一个在早上运行) 插入#ScheduleTest值('Chicago,IL,(Union Station)','ScheduleB',NULL,'06:00') 插入#ScheduleTest值('Chicago,IL,(DownTown)','ScheduleB','06:10','06:15') 插入#ScheduleTest值('Benton,IL,Harbor','ScheduleB','11:00',NULL)
选择 StationName,ScheduleNumber,ArrivalTime AS TimeOfEvent,'Arrival'AS [抵达/离开] INTO #tmp 从 #ScheduleTest 哪里 到货时间不是空的 UNION ALL 选择 StationName,ScheduleNumber,DepartureTime AS TimeOfEvent,'出发'AS [到达/离开] 从 #ScheduleTest 哪里 出发时间不是空的
- 由于枢轴的聚合要求,仅允许一个Schedule / Station组合。 - 这是核心数据的查询。现在,动态这个: 选择 站名, 安排一个, ScheduleB, [到达/离开] 从 #tmp AS t 枢 (MAX(TimeOfEvent)FOR ScheduleNumber IN(ScheduleA,ScheduleB) )AS pvt
DECLARE @sql nVARCHAR(MAX), @ScheduleNumber VARCHAR(50), @PivotInList nVARCHAR(MAX)
SET @sql ='SELECT StationName,' SET @PivotInList ='IN('
DECLARE ScheduleCursor CURSOR FAST_FORWARD FOR SELECT DISTINCT ScheduleNumber FROM #tmp
OPEN ScheduleCursor FETCH NEXT FROM ScheduleCursor INTO @ScheduleNumber
WHILE @@ FETCH_STATUS = 0 开始 SET @sql = @sql + @ScheduleNumber +',' SET @PivotInList = @PivotInList +''+ @ScheduleNumber +','
FETCH NEXT FROM ScheduleCursor INTO @ScheduleNumber
END
关闭ScheduleCursor DEALLOCATE ScheduleCursor
SET @PivotInList = LEFT(@PivotInList,LEN(@PivotInList) - 1 / 删除额外的逗号空间 /)+')' 打印@PivotInList
SET @sql = @sql +'[Arrival / Departure] FROM #tmp AS t PIVOT(MAX(TimeOfEvent)FOR ScheduleNumber' + @PivotInList +')AS pvt' PRINT @sql
EXEC sp_executeSQL @sql