0

表 1(修订版)

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 

笔记:

日期时间1 =DateAdd(hour, BufferPeriod/60, ResvDateTime)在哪里BufferInd = 1

日期时间2 =DateAdd(hour, BufferPeriod/60, ResvDateTime)在哪里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

我的 SQL 小提琴尝试(链接)

请帮助指出我的问题并告诉我应该如何完成枢轴和聚合功能。谢谢。

4

1 回答 1

2

笔记:

您的架构没有意义。如果您纯粹通过 ResvDT 列来组合 Reservations,那么它太善变了。对于旋转的目的,同时两个保留将变得无法区分。我假设您的 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 |

SQL小提琴

于 2013-05-17T03:18:18.030 回答