Aggregate/Combine 数据在一起

Aggregate/Combine Data together

我有一个 table 每个 ExternalID、TestCenter、Pricetype、日期和时间段的价格,看起来像下面每个时间段的价格。

SKU ExternalID TestCenterID pricetypeid timeslot dt_date TimePrice
1003.0113.01.01.06:00 1003 113 1 06:00:00.0000000 44713 569.00
1003.0113.01.01.06:20 1003 113 1 06:20:00.0000000 44713 569.00
1003.0113.01.01.07:00 1003 113 1 07:00:00.0000000 44713 539.00
1003.0113.01.01.07:20 1003 113 1 07:20:00.0000000 44713 539.00
1003.0113.01.01.09:40 1003 113 1 09:40:00.0000000 44713 539.00
1003.0113.01.01.10:00 1003 113 1 10:00:00.0000000 44713 539.00
1003.0113.01.01.10:20 1003 113 1 10:20:00.0000000 44713 449.00
1003.0113.01.01.10:40 1003 113 1 10:40:00.0000000 44713 539.00
1003.0113.01.01.14:20 1003 113 1 14:20:00.0000000 44713 449.00
1003.0113.01.01.14:40 1003 113 1 14:40:00.0000000 44713 539.00
1003.0113.01.01.16:00 1003 113 1 16:00:00.0000000 44713 569.00
1003.0113.01.01.16:20 1003 113 1 16:20:00.0000000 44713 569.00
1003.0113.01.03.06:00 1003 113 3 06:00:00.0000000 44713 619.00
1003.0113.01.03.16:20 1003 113 3 16:20:00.0000000 44713 619.00

我想做的是合并时间段,以便结束时间是价格变化的时间。因此,我不会写出 06.00 到 07.00 之间的每个时间段,而是写下该价格有效的开始时间和结束时间。

row_num SKU ExternalID TestCenterID pricetypeid timeslot dt_date TimePrice EndTime
1 1003.0113.01.01.06:00 1003 113 1 06:00:00.0000000 44713 569.00 07:00:00.0000000
2 1003.0113.01.01.07:00 1003 113 1 07:00:00.0000000 44713 539.00 10:20:00.0000000
3 1003.0113.01.01.10:20 1003 113 1 10:20:00.0000000 44713 449.00 10:40:00.0000000
4 1003.0113.01.01.10:40 1003 113 1 10:40:00.0000000 44713 539.00 14:20:00.0000000
5 1003.0113.01.01.14:20 1003 113 1 14:20:00.0000000 44713 449.00 14:40:00.0000000
6 1003.0113.01.01.14:40 1003 113 1 14:40:00.0000000 44713 539.00 16:00:00.0000000
7 1003.0113.01.01.16:00 1003 113 1 16:00:00.0000000 44713 569.00 23:59:00.0000000

改变的是 ExternalID、TestCenterID、Pricetypei、dt_date,当然还有时间段。

我已经按照正确的顺序对列表进行了排序,并为每个时间段添加了结束时间。但不幸的是我无法弄清楚如何根据第二个 table.

循环和聚合它

我想做的是创建一个 table 包含起始 sku 和结束时间。

Create table #SKUEND
(
    SKU varchar (25),
    ENDTIME time,
    iter int,
    row_num int,
);


DECLARE @Prices int;
DECLARE @row1 INT=1;
DECLARE @ITER INT=1;
DECLARE @SKU varchar(25);
DECLARE @ENDTIME time;
DECLARE @DT as date;
select @Prices=count(*) from #test

while @row1<@Prices

BEGIN

IF @ITER=1
begin
    SET @SKU=(select top 1 sku from #test where row_num=@row1)
    SET @DT=(select top 1 dt_date from #test where row_num=@row1)
    end

IF (SELECT TOP 1 TIMEPRICE from #test where row_num=@row1)<>(SELECT TOP 1 NextPrice from #test where row_num=@row1) 
BEGIN
IF (SELECT TOP 1 dt_date from #test where row_num=@row1)<>@DT
SET @ENDTIME='23:59:59'
else
SET @ENDTIME=(select top 1 EndTime from #test where row_num=@row1)
;

INSERT into #SKUEND 
VALUES (@SKU,@ENDTIME,@ITER,@row1)
SET @ITER=0

END


set @row1+=1
set @ITER+=1
END

这给了我结果:

SKU ENDTIME
1003.0113.01.01.06:00 07:00:00.0000000
1003.0113.01.01.07:00 10:20:00.0000000
1003.0113.01.01.10:20 10:40:00.0000000
1003.0113.01.01.10:40 14:20:00.0000000
1003.0113.01.01.14:20 14:40:00.0000000
1003.0113.01.01.14:40 16:00:00.0000000
1003.0113.01.01.16:00 23:59:59.0000000
1003.0113.02.01.07:00 10:20:00.0000000
1003.0113.02.01.10:20 10:40:00.0000000
1003.0113.02.01.10:40 14:20:00.0000000
1003.0113.02.01.14:20 14:40:00.0000000
1003.0113.02.01.14:40 16:00:00.0000000

我现在遇到的问题是,当 ExternalID、TestCenteriD、Pricetypeid 或 dt_date 发生变化时,它并没有真正重新开始。所以我想我需要对这些列进行循环。

试试这个查询:

WITH CTE1 AS
(
SELECT *,
CASE WHEN LAG(TimePrice,1,0.00) OVER(PARTITION BY ExternalID,TestCenterID,pricetypeid,dt_date  ORDER BY timeslot) = TimePrice THEN 0 ELSE 1 END AS flag
FROM Table1
)
SELECT ct1.SKU,ct1.ExternalID,ct1.TestCenterID,
ct1.pricetypeid,ct1.timeslot,ct1.dt_date,
ct1.TimePrice,
LEAD(timeslot,1,'23:59:00.0000000') OVER(PARTITION BY ExternalID,TestCenterID,pricetypeid,dt_date  ORDER BY timeslot) as EndTime
FROM CTE1 ct1
WHERE flag = 1;

db fiddle link