Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9223659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:07:30+00:00 2026-06-18T04:07:30+00:00

I have data in table as below : Three Columns date1 fromdate value ———————

  • 0

I have data in table as below :

Three Columns

 date1 fromdate value
 ---------------------
 14     0       150
 14     2       150
 14     3       150
 14     4       120
 14     5       120
 14     6       150
 14     7       150 

I need to write sql query to get data in below format:

 Date1 fromdate todate value
 ----------------------------
 14     0       3      150
 14     4       5      120
 14     6       7      150

Is this possible to do with SQL query in oracle ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:07:32+00:00Added an answer on June 18, 2026 at 4:07 am

    You need to split your rows with the value 150 into two separate sections. You can do this by using analytic functions. The lead function allows you to look at the value of the next row by some order. In this case you want to find when you have a different value order by your fromdate:

    select lead(value) over (order by fromdate) from tab;
    

    You can then compare this to the current value to see when they change, outputting a discriminator when they do:

    select value, case when ld <> value or ld is null then rownum end c from (
      select value, lead(value) over (order by fromdate) ld from tab
    )
    
    VALUE, C
    -----------
    150    null
    150    null
    150    3
    120    null
    120    5
    150    null
    150    7
    

    You then need to “fill out” the discriminator to replace the nulls. This can be done with analytics again, selecting the min discriminator value by fromdate descending. You can then group by this value to give you the output you want:

    select date1, value, d, min(fromdate), max(fromdate)
    from (
      select date1, fromdate, value,
             --"fill down" the discriminator, so you can group by it at the next level 
             min(c) over (order by fromdate desc) d 
      from (
        select date1, fromdate, value, ld, 
               --compare the next value with the current, if they're different
               --show an indicator
               case when value <> ld or ld is null then rownum else null end c
        from (
          select date1, fromdate, value,lead(value) over (order by fromdate) ld
          from tab
        )
      )
    )
    group by date1, value, d
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I do have a table data as shown below: <td> <label for=title>Title : </label>
I have a piece of matlab code below which reads data from a table.
I have a table that has data like below(sorted by date) COL_A | COL_B
I have a table part with a few demo data as below in Oracle
I have a data table with many rows and columns. How can I display
i have a table named daily_mood and it has three (3) columns, looks like
I have a large table with sales data, useful data below: RowID Date Customer
I have two table in my data base As below: CUSTOMER ( ID integer
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.