用下面的數據組數據由分組列值的變化
create table #ph (product int, [date] date, price int)
insert into #ph select 1, '20120101', 1
insert into #ph select 1, '20120102', 1
insert into #ph select 1, '20120103', 1
insert into #ph select 1, '20120104', 1
insert into #ph select 1, '20120105', 2
insert into #ph select 1, '20120106', 2
insert into #ph select 1, '20120107', 2
insert into #ph select 1, '20120108', 2
insert into #ph select 1, '20120109', 1
insert into #ph select 1, '20120110', 1
insert into #ph select 1, '20120111', 1
insert into #ph select 1, '20120112', 1
我想產生以下輸出:
product | date_from | date_to | price
1 | 20120101 | 20120105 | 1
1 | 20120105 | 20120109 | 2
1 | 20120109 | 20120112 | 1
如果我組由價格,並顯示最大和最短日期,然後我會得到以下這是不是我想要的(見日期重疊)。
product | date_from | date_to | price
1 | 20120101 | 20120112 | 1
1 | 20120105 | 20120108 | 2
所以基本上我想要做的就是根據組列產品和價格的數據的階躍變化。
達到此目的的最簡潔方法是什麼?
這是一個實例所謂的'差距和島嶼'問題,fyi。 – AakashM 2012-04-11 16:32:04
@AakashM會看一看,我試過搜索,但沒有這個問題的明確定義。謝謝 – MrEdmundo 2012-04-11 16:34:21
np。有一個'數字表'(在這種情況下,'日期表')將非常有幫助。 – AakashM 2012-04-11 16:36:08