开发者

Need to perform a select which unrolls data

开发者 https://www.devze.com 2023-04-06 16:38 出处:网络
please help, tried different queries with no luck in Oracle environment. To simplify th开发者_JAVA技巧e case, assume that we have one table with records like this

please help, tried different queries with no luck in Oracle environment. To simplify th开发者_JAVA技巧e case, assume that we have one table with records like this

doc_id   doc_date  product_id price
1       01.01.2011    1       20.3
1       01.01.2011    2       10.0
2       15.01.2011    3       10.3
2       15.01.2011    2       null

So, the price for product is set by document from doc_date till next doc_date or to infinity if no more document which sets the new price or reset it to null exists, null means that product sales is not available starting from doc_date.

I need a select which "unrolls" the data in the form:

doc_id   from_date   to_date    product_id price
1        01.01.2011  null          1        20.3
1        01.01.2011  14.01.2011    2        10.0
2        15.01.2011  null          2        null
2        15.01.2011  null          3        10.3

or even:

doc_id   from_date   to_date    product_id price
1        01.01.2011  14.01.2011    1        20.3
1        01.01.2011  14.01.2011    2        10.0
2        15.01.2011  null          2        null
2        15.01.2011  null          3        10.3
any      15.01.2011  null          1        20.3

Thanks!


Select 
  doc_id, 
  doc_date as from_date,
  (lead(doc_date) over (partition by product_id order by doc_date) ) - 1  as to_date,
  product_id, 
  price
from
  product_table
0

精彩评论

暂无评论...
验证码 换一张
取 消