Selecting min date in a group

Posted on 2011-05-11
Last Modified: 2012-05-11
I want to be able to group data but having the ability to be able to return data subject to: Col B > the min date + 1 month in each grouping.
Example data set.....
ColA         ColB
A              01/01/2001
A              01/02/2001
A              01/03/2001
A              01/04/2001
A              01/05/2001
B              31/05/2006
B              30/06/2006
B              31/07/2006

so, if I group on column A and the above logic applied where in each group set the min date in each group + 1 month would now return the following...

A              01/02/2001
A              01/03/2001
A              01/04/2001
A              01/05/2001
B              30/06/2006
B              31/07/2006

Question by:philsivyer
    LVL 3

    Expert Comment

    hi would you clarify:
    the example table has: A              01/01/2001
    but out put doesn't,

    Author Comment

    The min date for each group is
    A 01/01/2001
    B 31/05/2006

    The data returned for each grouping should be anything greater of the min date by one month for each group
    LVL 3

    Accepted Solution

    select tbl.* FROM Table_1 tbl
    INNER JOIN (SELECT MIN(ColB) md, ColA gr FROM Table_1  group by ColA) temp on tbl.ColB>=DateAdd(Month, 1,

    Open in new window

    this is sql server code, but think is the same in oracle
    maybe will change something, but the concept is the same

    hope it helps

    Author Comment

    LVL 20

    Expert Comment

    select table11.col1, table11.col2
    from table11,
    (select  col1 , add_months(min(col2),1) col2
     from table11
     group by col1 ) min
    where  table11.col1 = min.col1
    and table11.col2 >= min.col2
    order by table11.col1, table11.col2

    check if the add_months does what you want

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
    Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
    Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
    This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now