Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

oracle12c - Adding days to the select query result dates in oracle

select from_date_su_rela,to_date_su_rela from RELATION_T;

to_date_su_rela+56

But the expectation is reducing -56 days from the first row from_date_su_rela and adding the +56 days to last(3rd) row to_date_su_rela. as below

enter image description here

I have written query as ,

select from_date_su_rela-56,to_date_su_rela+56 from RELATION_T; But its adding and reducing days from all the rows as below,

enter image description here

How to make it working as above 2nd image.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One option is to use the row_number analytic function sorting the data both ascending and descending to find the first and last row and then perform the addition and subtraction in a case statement

select case when rn_asc = 1
            then from_date_su_rela - 56
            else from_date_su_rela
         end from_date_su_rela,
       case when rn_desc = 1
            then to_date_su_rela + 56
            else to_date_su_rela
         end to_date_su_rela
  from (
      select from_date_su_rela,
             to_date_su_rela,
             row_number() over (order by from_date_su_rela desc) rn_desc,
             row_number() over (order by from_date_su_rela asc) rn_asc
        from relation_t
  )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...