How to merge two date ranges

Hi,
I have following table. I want to merge date ranges if the dates are continuous and the value is same.I am using Oracle 10g.Please help.
START_DATE END_DATE VALUE
1/1/2008 12/31/2008 1234
1/1/2009 12/31/2009 1234
Sql statement output should be as follows:
START_DATE END_DATE VALUE
1/1/2008 12/31/2009 1234
Thanks in advance.
Edited by: user3898545 on Jan 27, 2010 7:44 PM

Hi,
user3898545 wrote:
create table test_group(emp_id varchar2(5),
start_date date,
end_date date,
value varchar2(10));
insert into test_group(emp_id,start_date,end_date,VALUE) values ('E1','1-JAN-2008','31-DEC-2008','G1');start_date and end_date are DATEs; don't try to put VARCHAR2 values (such as '1-JAN-2008') into DATE columns. Use TO_DATE or DATE literals instead. The INSERT statements you posted may work on your system, but they won't necessarily work anywhere else. In particular, they don't work on my system. Post some sample data that anyone can use.
... I used following query to get the output 1/1/2009 - 12/31/2009 G2 when I ran for any date in 2010 or greater.There can be break in date ranges. Although query is giving the correct output which I am looking for I am looking for better performence.
SELECT MAX (START_DATE)     AS START_DATE,MAX (END_DATE) AS END_DATE ,
MAX(VALUE) KEEP (DENSE_RANK FIRST ORDER BY START_DATE DESC) GROUP_NUMBER
FROM
(WITH     GOT_GRP     AS
(SELECT     TB_TEST_DATA.*,
     END_DATE - SUM (END_DATE + 1 - START_DATE) OVER ( PARTITION BY VALUE
                                        ORDER BY START_DATE) AS GRP
FROM
(SELECT HD.START_DATE,NVL(HD.END_DATE,'31-DEC-9999') "END_DATE",HD.VALUE
FROM test_group HD
WHERE HD.EMP_ID = 'E1'
AND HD.START_DATE < NVL (HD.END_DATE, '31-DEC-9999')
AND HD.END_DATE < :VDT_AS_OF_DATE
ORDER BY EMP_ID,START_DATE) TB_TEST_DATA
SELECT MIN (START_DATE)     AS START_DATE,MAX (END_DATE) AS END_DATE,VALUE
FROM GOT_GRP
GROUP BY VALUE,GRP
ORDER BY START_DATE) CO_GRP;If you have new requirements, maybe you should start a new thread.
What are your new requirements?
When you say "There can be break in date ranges", do you mean grp plays no role in this new problem? In that case, you can save time and effort by not computing it.
The following should produce the results you want from the sampe data given:
WITH     got_r_num     AS
     SELECT     start_date, end_date, value
     ,     ROW_NUMBER () OVER ( ORDER BY start_date  DESC
                        ,           value       DESC
                       )     AS r_num
     FROM     test_group
     WHERE     end_date < TO_DATE ( :vdt_as_of_date
                       , 'DD-MON-YYYY'
SELECT     start_date, end_date, value
FROM     got_r_num
WHERE     r_num     = 1
;

Similar Messages

  • How to merge two Outlook 2010 pst data files?

    How to merge two Outlook 2010 pst data files?

    Easy way: with the one you want to use open in Outlook and the other one not in your profile, go to File, Open, Import.
    If you only want to merge some content, open both pst files in outlook and drag between folders. For calendar, use a list view to see all of the appointments.
    Diane Poremsky [MVP - Outlook]
    Outlook Daily Tips |
    Outlook & Exchange Solutions Center
    Subscribe to Exchange Messaging Outlook weekly newsletter

  • Select Between two date ranges from xml file

    Hi ,
    I have a column date_ with datatype VARCHAR2(150) it stores
    data as
    05/19/2010 11:23 AM
    05/20/2010 12:23 PM
    05/22/2010 11:23 AM
    05/25/2010 11:23 AM
    i have to select all the rows between 05/19/2010 and 05/22/2010 how to do that this column is in xml file

    I have a table wit two fields
    Field1 is integer and field2 is xmltype
    in the xmltype i store an xml file
    <ParentNode>
    <Node>
    <Cat>1</Cat>
    <Date>05/19/2010 11:23 AM </Date>
    </Node>
    <Node>
    <Cat>2</Cat>
    <Date>05/20/2010 12:23 PM </Date>
    </Node>
    <Node>
    <Cat>3</Cat>
    <Date>05/22/2010 11:23 AM </Date>
    </Node>
    </Parentnode>
    I am using teh below query to retrive teh result
    SELECT T.Feild1, XML.* FROM Tablename T,
    XMLTable( 'Parentnod/Node' PASSING T.Feild2 COLUMNS Cat NUMBER PATH 'Cat' ,
    DATE_ VARCHAR2(100) PATH 'Date'
    )XML where cat >1;
    now i have to do teh same to select the rows between two date range 05/19/2010 and 05/21/2010
    hope i am able to make teh question simple

  • How to merge two adjacent partitions?

    How to merge two adjacent partitions?

    The formal steps are delete the second partition and right click on first one, choose "extend".
    Of course this will erase data on second partition.
    Some third party tools can help do the job with keep data on second partition. 
    If you have any feedback on our support, please send to [email protected]

  • How to merge two conflicting contact on iPhone and on laptop?

    How to merge two conflicting contacts?
    When syncing my iPhone w/ my Mac laptop, it seems that Apple does not provide an option that allows you to merge the data from two conflicting contacts.  For example, you have a contact on your iPhone for Andy Summer and a contact on your laptop for Andy Summers - one contact contains only Andy's name and email address, the other contact contains only Andy's name and his mailing address.  I'd like to be able to merge the data from these two contacts into one and save it to both, but the Conflict Resolver in the syncing program (I think it's in iTunes) does not offer an option for merging the two.
    Does anyone have any knowledge about 1) how to merge data from two conflicting contacts, or 2) some other way to keep the data from each so that I don't have to choose one over the other?

    Jawad,
    First of all,You need to make teaming on both the interfaces on the server side.Which mode do you want to use? Active-Active or Active-Standby.
    Server side :Active-Active - You need to configure the switch side to be a etherchannel/LACP. It can be done by the commands on the cisco 4006.
    Server side :Active-Standby - You need nothing to do at the switch side.
    Note: Seems PAGP is a proprietary of cisco. Server shouldn't know that. To be bound with mode "on" of "lacp" should be fine. It depends on the server side as well.
    HTH
    Thot

  • How to set a date range for date field ?

    Dear Experts,
    Scenario:
    I have a query in validating the date field in my BSP application. My application is for maintain infotype 0023 Other/Previous Employers online by employees in the company.
    As per our design we are maintaining the all employment details of the employee both ( with in the current company / previous employment outside the company) in the same infotype.
    Every employee will have a hiring date within the SAP HR system. We consider this date as the cutoff date between current and previous employment in our application. When the employee updating the details wia BSP page I need to check the following.
    Record inside current company: Validation that, the user should only able to enter BEGIN DATE (BEGDA)  greater than or equal HIRING DATE and END DATE(ENDDA) should be greater than FROM  DATE (BEGDA).
    Record outside current Company: Validation that, the user should only able to enter BEGIN DATE (BEGDA)  less than or equal HIRING DATE and END DATE (ENDDA) should be greater than FROM  DATE (BEGDA) and less than HIRING DATE.
    Technical Requirement:
    How to set a date range for date field, i.e. how we can limit the date range in a HTMLB date field? Can this it be achieved via standard functionality of HTMLB?
    Following is the code to describe date field in my application.
        <htmlb:inputField id= "ENDDA_NEW_IN"
                          type= "date"
                    doValidate= "TRUE"
                      showHelp= "TRUE"
                      disabled= "FALSE"
                         width= "183"
                         style= "cssTextAreadate"
                         value= "<%='99991231'%>"/>
    Thanks a lot in advance for your assistance and help.
    Cibinu2026
    Edited by: cibin kuruvilla on Nov 12, 2008 11:13 AM

    Hi,
    This functionality is known to be very important and is a key part of the next major release of the JRC planned for the first half of 2008.
    Regards,
    <p>Blair Wheadon</p>
    <p>Product Manager, Crystal Reports</p>

  • How to merge two repositories using(MUD)

    hi all,
    how to merge two repositories using mude, for example i have sh rpd and paint rpd how to merge two repositories using Multi user development(mud), please don't give venkat forums link and oracle's training doc links.
    Is this possible? if it is possible please help me .
    Thanks
    Naresh

    Hi Naresh,
    What you could do is:
    1. do a ontime command line merge of both repository using the nqudmlexec tool
    2. In the mergerd repository created two project (sh and paint) and assign one bussiness model to each project.
    3. Make this the master repository for your MUD.
    4. Check the projects in and out.
    regards
    John
    http://obiee101.blogspot.com

  • How to merge two search button from different criteria

    How to merge two search button from different criteria
    this image show the question
    http://s24.postimg.org/4algthuj9/1111.jpg
    two different criteria for the same view and I need to merge it in one button as when I click
    on the button search result give from two criteria parameters

    You can!t using af:query. the af:query component comes as is. If you can't do it through the properties, you probably can't do it at all.
    As in your other thread, the way to go is to program your own search form. This way you can use a layout and functionality you like.
    For this you create a form with input fields for the values you are searching for, put them on hte page in a way you like and in the end by hitting on a button (e.g. called Search) map all inputfields to parameters of a service method you defined in your application module or VO. This service method then executes a view criteria returning all matches in the default iterator (just as if you had used af:query.
    Timo 

  • How to Compare two Dates in java

    How to Compare two Date Field after getting the values from jTextField1.getText() and jTextField2.getText().

    Date d1=DateFormat.getDateInstance().parse(yourstring1);
    same for d2
    d1.compareTo(d2);
    could be that i misrememberd the exact naems of some functions or mixed up something in the equence of d1=

  • t:inputCalendar How To Specify a Date Range?

    <-- Sorry for my english. -->
    Can anybody help me?
    I want to know how to specify a date range in the <t:inputCalendar/> component. I'm using Tomahawk/Apche implementation. So that the user cannot specify a date outside that range.
    Thanks.

    I don't think that the inputCalendar tag supports that directly. However, you could add a validator to the component.

  • How to compare two dates that should not exceed morethan 3 years

    hi all,
    can you please tell me how to compare two dates( basically dates are string type)
    that should not exceed more than 3 years? in JAVASCRIPT
    Thanks in Advance.

    This is not a JavaScript forum.
    [*Google* JavaScript Forum|http://www.google.co.uk/search?q=JavaScript+Forum&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-GB:official&client=firefox-a]
    Good luck.

  • How to compare two dates to know which one is greater than oher?

    how to compare two dates to know which one is greater than oher?
    Please search before asking basic questions.
    Edited by: Rob Burbank on Mar 27, 2009 9:26 AM

    Hi,
    If thse to date fields are of same type u can directly compare like this.
    regards,
    Bharat.

  • How to merge two XML's with JDOM?

    How to merge two xmls's using jdom? like:
    is there a way to add the complete content one xml into the parent node of another xml?
    //in.xml
    <?xml version="1.0"?>
    <people>
    <person>
      <name>ABC</name>
      <email>[email protected]</email>
    </person>
    </people>
    //out.xml
    <?xml version="1.0"?>
    <address>
    <city> abccounty</city>
    <state> abcstate</state>
    </address>
    Merged XML:
    <?xml version="1.0"?>
    <people>
    <person>
      <name>xyz</name>
      <email>[email protected]</email>
    </person>
    <address>
    <city> abccounty</city>
    <state> abcstate</state>
    </address>
    </people>
    import java.util.List;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;
    public class MergeXMLS {
         public static void main(String[] args) {
              try{
                  SAXBuilder builder = new SAXBuilder();
                  Document books = builder.build("D:/in.xml");
                  Document onebook = builder.build("D:/out.xml");
                  Element root = books.getRootElement();
                  List rows = root.getChildren();
                  for (int i = 0; i < rows.size(); i++) {
                      Element row = (Element) rows.get(i);
                      onebook.getRootElement().addContent(row.detach());
                      System.out.println(row.getName());
                  new XMLOutputter(Format.getPrettyFormat()).output(onebook, System.out);
              }catch(Exception e){
                   e.printStackTrace();
    }

    The above code only add's the first node.
    I changed the code little differently to
                  SAXBuilder builder = new SAXBuilder();
                  Document books = builder.build("D:/in.xml");
                  Document onebook = builder.build("D:/out.xml");
                 //trying to add second xml into the first
                  books.getRootElement().addContent(onebook.getRootElement().getContent()); 
                  new XMLOutputter(Format.getPrettyFormat()).output(books, System.out);
    here is the exception from the above code:
    org.jdom.IllegalAddException: The Content already has an existing parent "address"
         at org.jdom.ContentList.add(ContentList.java:218)
         at org.jdom.ContentList.add(ContentList.java:140)

  • [Help] How to merge two rows in a datagrid?

    How to merge two rows of a specific column in a datagrid?
    ex.
    |_______|_____|_____|
    | merged |_____|_____|
    |_______|_____|_____|
    |_______|_____|_____|
    thx a lot~

    I need to merge column heading. Have u find any solution for
    ur problem?

  • How to merge two cells in a row

    hi freinds,
    how to merge two cells in alv grid using oops.
    for eg ,in the first row there is text premraj
             n in the second row there is cheguri
              my requriment is two merge the two cells ie to remove the horizontal line between them
    thanks & regards,
    premraj

    Well, as a new member you should learn to search the forum first before posting questions. Using keywords like "jtable merge cell" will find other postings on this topic. Some with solutions some without, so you will need to read a few and decide on the approach you want to take.

Maybe you are looking for

  • Problem getting blank DVDs to show up on desktop or in iTunes

    Hello, I have been trying to burn playlists in itunes onto a blank dvd. I can burn CDs, but when I try to insert a blank DVD, it checks media and spits it back out. Neither will my MAC show the blank dvd on my desktop when I insert it outside of iTun

  • How can I make Elements 9 my default to open picture files with?

    After using Elements 12 for a months trial and then uninstalling it, I can no longer find Elements 9 listed when I right click on a picture file and select "open with".  How can I restore it to my list of programs with which I can open the file.  I'm

  • CUA problem with composite role

    Hello experts, I have a problem with a composite role in my CUA parent system. If you look at the roles tab you will see one of the child roles has a name of child CUA system in the 'target sys' column. the rest all have 'user system'. Can anyone exp

  • Why are songs missing from albums I downloaded

    When I try to listen to some albums songs are missing! Do I have to download the entire cd again?

  • Duplicate Entries in Application List ..?? 8230 OS4.6 DTM 6.2 Win XP SP3

    Hi ... I just noticed that the Application list in DTM shows duplicate entries of 3 of my apps on my 8230 ... I was curious about what was listed, because I was investigating the phenomenon of MyWorld incorrectly showing the "trial" verions of 2 apps