表1(Resv)
ResvID | ResvDateTime | BufferInd
-----------------------------------------------
1 | 2012-06-11 08:30:00.000 | 1
2 | 2012-06-11 08:30:00.000 | 2
4 | 2013-07-20 12:00:00.000 | 1
5 | 2013-07-20 12:00:00.000 | 2
注意:ResvID(int identity)
,ResvDateTime(datetime)
表2(缓冲区)
BufferInd | BufferPeriod (minutes)
---------------------------------
1 | 60
2 | 120
注意:BufferInd(int)
,BufferPeriod(int)
我想使用pivot
将这两个表与SQL视图
ResvID | ResvDateTime | BufferInd1 | BufferInd2
---------------------------------------------------------------------
1 | 2012-06-11 08:30:00.000 | 1 | 2
2 | 2013-07-20 12:00:00.000 | 1 | 2
ResvID | ResvDateTime | DateTimeAfterOneHour | DateTimeAfterTwoHour
--------------------------------------------------------------------------------------
1 | 2012-06-11 08:30:00.000 | 2012-06-11 09:30:00.000 | 2012-06-11 10:30:00.000
2 | 2013-07-20 12:00:00.000 | 2013-07-20 13:00:00.000 | 2013-07-20 14:00:00.000
注意:
DateTime1 = DateAdd(hour, BufferPeriod/60, ResvDateTime)
WHERE BufferInd = 1
DateTime2 = DateAdd(hour, BufferPeriod/60, ResvDateTime)
WHERE BufferInd = 2
这是我在表中声明所有变量的尝试,它失败了。
Create Table Resv
([ResvID] int, [BufferTypeInd] int ,[ResvDT] datetime, [BufferPeriod] int)
;
Insert Into Resv
([ResvID], [BufferTypeInd], [ResvDT], [BufferPeriod])
Values
(1, 1, '2012-06-11 08:30:00.000', 60),
(2, 2, '2012-06-11 08:30:00.000', 180),
(4, 1, '2013-07-20 12:00:00.000', 60),
(5, 2, '2013-07-20 12:00:00.000', 180),
;
我的支点尝试:
SELECT *
FROM
(
(SELECT
[BufferTypeInd], [ResvDT], [BufferPeriod]
FROM Resv)
) AS source
PIVOT
(
MAX([ResvDT])
FOR [BufferTypeInd] IN ([1],[2],[3])
) as pvt;
SELECT ResvID,
MAX(
CASE WHEN
BufferTypeInd = '1'
THEN
DateAdd(hour, BufferPeriod, ResvDT)
ELSE
NULL
END) [1],
MAX(
CASE WHEN
BufferTypeInd = '2'
THEN
DateAdd(hour, BufferPeriod, ResvDT)
ELSE
NULL
END) [2]
FROM Resv
GROUP BY ResvID
请帮助指出我的问题并告诉我如何完成枢轴和聚合功能。谢谢。
答案 0 :(得分:2)
注意:
您的架构没有意义。如果您仅仅通过ResvDT列合并预订,那么它就变得多变。为了转动的目的,两个同时保留将变得无法区分。我假设您的ResvID列对于相同的预留是相同的。否则,如果ResvDT本身是唯一的,您可以在下面的查询中从PIVOT源中删除它。
数据
Create Table Resv
([ResvID] int, [ResvDT] datetime, [BufferTypeInd] int)
;
Insert Into Resv
([ResvID], [ResvDT], [BufferTypeInd])
Values
(1, '2012-06-11 08:30:00.000', 1),
(1, '2012-06-11 08:30:00.000', 3),
(1, '2012-06-11 08:30:00.000', 4),
(2, '2013-07-20 12:00:00.000', 1),
(2, '2013-07-20 12:00:00.000', 3),
(2, '2013-07-20 12:00:00.000', 4)
;
Create Table Buffer
(BufferTypeInd int, BufferPeriod int)
;
Insert into Buffer values
(1, 60),
(3, 180),
(4, 240);
查询
declare @cols nvarchar(max), @names nvarchar(max);
select @cols = isnull(@cols + ',', '')
+ QuoteName(RTrim(BufferPeriod)),
@names = isnull(@names + ',', '')
+ QuoteName(RTrim(BufferPeriod))
+ ' as DateTimeAfter' + RTrim(BufferPeriod) + 'Minutes'
from Buffer
order by BufferPeriod;
declare @nsql nvarchar(max);
select @nsql = N'
SELECT ResvID, ResvDT, ' + @names + '
FROM
(
(SELECT R.ResvID, R.[ResvDT], B.[BufferPeriod],
DateAdd(minute,B.BufferPeriod,R.ResvDT) TimeAfter
FROM Resv R
JOIN Buffer B on B.BufferTypeInd = R.BufferTypeInd)
) AS source
PIVOT
(
MAX(TimeAfter)
FOR [BufferPeriod] IN (' + @cols + ')
) as pvt';
exec (@nsql);
结果
| RESVID | RESVDT | DATETIMEAFTER60MINUTES | DATETIMEAFTER180MINUTES | DATETIMEAFTER240MINUTES |
----------------------------------------------------------------------------------------------------------------------------------
| 1 | June, 11 2012 08:30:00+0000 | June, 11 2012 09:30:00+0000 | June, 11 2012 11:30:00+0000 | June, 11 2012 12:30:00+0000 |
| 2 | July, 20 2013 12:00:00+0000 | July, 20 2013 13:00:00+0000 | July, 20 2013 15:00:00+0000 | July, 20 2013 16:00:00+0000 |