[英]Previous row end date as the next row start date in SQL
需要一些幫助請,
我有一個名為“hist_lastupdated”的字段,其中包含產品價格修改的最后更新日期。
基於此字段,我想提取修改的開始日期和結束日期。
事實上我有這個:
**Product_id , Price , hist_lastupdated**
284849 18.95 2015-05-29 00:53:55
284849 15.95 2015-08-14 01:04:46
284849 18.95 2016-06-11 00:50:31
284849 15.95 2016-08-24 00:45:11
我想得到這樣的結果:
**Product_id , Price , hist_lastupdated ,start_date , End_date**
284849 18.95 2015-05-29 00:53:55 2014-05-01 00:00:00 2015-05-29 00:53:55
284849 15.95 2015-08-14 01:04:46 2015-05-29 00:53:55 2015-08-14 01:04:46
284849 18.95 2016-06-11 00:50:31 2015-08-14 01:04:46 2016-06-11 00:50:31
284849 15.95 2016-08-24 00:45:11 2016-06-11 00:50:31 2016-08-24 00:45:11
兩個字,開始日期是上一行的結束日期我有很多產品id
像這樣的東西:
select Product_id,
Price,
hist_lastupdated,
lag(hist_lastupdated) over (partition by product_id order by hist_lastupdated) as start_date,
hist_lastupdated as end_date
from the_table
您沒有解釋第一列的 start_date 是在哪里計算的。 如果那是從hist_lastupdated
開始的月初,您可以執行以下操作:
lag(hist_lastupdated, 1, date_trunc('month', hist_lastupdated)) over (...)
我不確定您將如何僅使用 SQL 來執行此操作,但是如果您能夠編寫一些腳本,則可以編寫一個類似於以下內容的快速程序(偽代碼):
lines = execute(SELECT product_id, price, hist_lastupdated FROM ProductTable)
startDate = 00:00:00 2014-05-01
outputLines = []
for row in lines:
outLine = []
outline.append(row[0])
outline.append(row[1])
outline.append(row[2])
outline.append(startDate)
outline.append(row[2])
startDate = row[2]
#Now do what you want with the output you have in a nice list of lists in the format you need, insert into a different table, write to a file, whatever you want.
我會在 MS SQL Server 中使用這些解決方案之一。 希望其中之一適用於您的問題。
純 SQL 語句如下所示:
select
t.product_id, t.price, s.start_date, t.end_date
from
product t
outer apply
(
select top 1
end_date start_date
from
product o
where
o.end_date < t.end_date
order by
o.end_date desc
) s
即使具有良好的索引,對返回的每條記錄的交叉應用也可能是一個性能問題。
如果您的 SQL 服務器支持 LAG 功能:
select
t.product_id, t.price,
LAG(T.end_date) over (order by t.end_date),
t.end_date
from
product t
或者您可能會找到一種方法對更新語句中的變量執行相同的操作,以“記住”先前更新記錄中的值,如 T-SQL:
-- Insert the desired output into a table variable that also has a start_date field.
-- Be sure to insert the records ordered by the date value.
declare @output table (product_id int, price numeric(10,2), [start_date] datetime, [end_date] datetime)
insert @output (product_id, price, end_date)
select 1, 10, '1/1/2015'
union all select 2, 11, '2/1/2015'
union all select 3, 15, '3/1/2015'
union all select 4, 20, '4/1/2015'
order by 3
-- Update the start date using the end date from the previous record
declare @start_date datetime, @end_date datetime
update
@output
set
@start_date = @end_date,
@end_date = end_date,
start_date = @start_date
select * from @output
我認為 Microsoft 不推薦這種技術,但它對我很有用並且一直有效。 我只對表變量使用了這種技術。 我不太願意相信實際表中記錄的更新順序。 現在我會改用 LAG() 。
這是我找到的解決方案,我想使用滯后功能,但結果不是我想要的。
解決方案 :
WITH
price_table_1 as (
select
-1 + ROW_NUMBER() over (partition by t1.product_id,t1.id ,t1.channel_id) as rownum_w1,
t1.id,
t1.product_id,
t1.channel_id,
t1.member_id,
t1.quantity,
t1.price,
t1.promo_dt_start,
t1.promo_dt_end,
t1.hist_lastupdated
FROM dwh_prod.hist_prices t1
where t1.channel_id='1004' and t1.product_id = '5896' and t1.quantity = '1' and t1.promo_dt_start is null
order by t1.product_id,t1.channel_id,t1.hist_lastupdated
),price_table_2 as (
select
ROW_NUMBER() over (partition by t2.product_id,t2.id ,t2.channel_id) as rownum_w2,
t2.id,
t2.product_id,
t2.channel_id,
t2.member_id,
t2.quantity,
t2.price,
t2.promo_dt_start,
t2.promo_dt_end,
t2.hist_lastupdated
FROM dwh_prod.hist_prices t2
where t2.channel_id='1004' and t2.product_id = '5896' and t2.quantity = '1' and t2.promo_dt_start is null
order by t2.product_id,t2.channel_id,t2.hist_lastupdated
)
select
t1.id,
t1.product_id,
t1.channel_id,
t1.member_id,
t1.quantity,
t1.price,
t1.promo_dt_start,
t1.promo_dt_end,
t2.hist_lastupdated as start_date,
t1.hist_lastupdated as end_date
FROM price_table_1 t1
inner join price_table_2 t2
on t2.product_id = t1.product_id and t2.id = t1.id and t2.channel_id = t1.channel_id
and rownum_w1 = (rownum_w2)
UNION ALL
select
t1.id,
t1.product_id,
t1.channel_id,
t1.member_id,
t1.quantity,
t1.price,
t1.promo_dt_start,
t1.promo_dt_end,
CONVERT(TIMESTAMP,'2014-01-01') as start_date,
t1.hist_lastupdated as end_date
FROM price_table_1 t1
where rownum_w1 = '0';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.