将行重复另一个时间

时间:2018-09-26 10:09:10

标签: sql sql-server tsql

我有一个查询,如下所示:

select cast(DATEADD(day,-1,DATEADD(hour,1, INTERVAL_PERIOD_TIMESTAMP)) as date) as 'TradeDate'
   , 'P' as 'Currency' 
   , cast(DATEADD(hour,1, INTERVAL_PERIOD_TIMESTAMP) as date) as 'DeliveryDate'
   , cast(INTERVAL_PERIOD_TIMESTAMP as time) as 'DeliveryTime'
   , cast(CLEARED_DAM_PRICE as decimal(16,2)) as 'SMP'
   , 0 as 'TimeChange'
   , GETDATE() as 'DateAdded'
   from DAM_RESULT

这将返回结果列表,如下所示:

2018-08-11  P   2018-08-12  23:00:00.0000000    37.12   0   2018-09-26 10:52:27.157
2018-08-11  P   2018-08-12  00:00:00.0000000    37.12   0   2018-09-26 10:52:27.157
2018-08-11  P   2018-08-12  01:00:00.0000000    37.12   0   2018-09-26 10:52:27.157
2018-08-11  P   2018-08-12  02:00:00.0000000    35.86   0   2018-09-26 10:52:27.157
2018-08-11  P   2018-08-12  03:00:00.0000000    35.79   0   2018-09-26 10:52:27.157
2018-08-11  P   2018-08-12  04:00:00.0000000    37.12   0   2018-09-26 10:52:27.157

目前,我每隔一个小时便会获得一次粒度,但是我尝试与之集成的先前数据模型是基于半小时粒度。下面的值显示23:00是37.12,如何修改查询,以便以相同的价格添加23:30小时的另一行?

是否有比通过增加30分钟的时间并连接输出的两个表更好的选择了?

1 个答案:

答案 0 :(得分:1)

我会在其他分钟内添加。如果我理解正确:

select cast(DATEADD(day, -1, DATEADD(hour, 1, dr.INTERVAL_PERIOD_TIMESTAMP)) as date) as TradeDate,
       'P' as Currency,
       cast(DATEADD(minute, 60 + v.additional_minutes, dr.INTERVAL_PERIOD_TIMESTAMP) as date) as DeliveryDate,
       cast(dr.INTERVAL_PERIOD_TIMESTAMP as time) as DeliveryTime,
       cast(dr.CLEARED_DAM_PRICE as decimal(16, 2)) as SMP,
       0 as TimeChange,
       GETDATE() as DateAdded
from DAM_RESULT dr CROSS JOIN
     (VALUES (0), (30)) as v(additional_minutes);

请注意,我添加了一个表别名并限定了所有列引用。另外,我从列名中删除了单引号。仅对字符串和日期常量使用单引号。