Skip to Content
author's profile photo Former Member
Former Member

SQL Question: Selecting earliest date time

I've a table that contains multiple transactions per any given day.

For each day of the month, I'm trying to find the earliest transaction possible. Below is my table data:

---------------------------------------------------------------
DATE                    |QUANTITY    |DIVISION    |  LOCATION
---------------------------------------------------------------
01-JAN-2008 12:42:01 AM |1234        |1           |  NORTH
01-JAN-2008 05:42:22 AM |1200        |1           |  NORTH
01-FEB-2008 01:42:01 AM |1123        |1           |  NORTH
01-FEB-2008 03:11:01 AM |985         |1           |  NORTH

So the question is, how do I select the EARLIEST transaction row for each given month,

This is my current SQL:

select       min(DATE),
             QUANTITY,
             DIVISION,
             LOCATION
             from TABLE
             group by QUANTITY, DIVISION, LOCATION

DATE field is a datatype "DATE" in Oracle database

My goal is to pretty much have the results shown as follows:

---------------------------------------------------------------
DATE                    |QUANTITY    |DIVISION    |  LOCATION
---------------------------------------------------------------
01-JAN-2008 12:42:01 AM |1234        |1           |  NORTH
01-FEB-2008 01:42:01 AM |1123        |1           |  NORTH

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Sep 19, 2008 at 03:22 PM

    Hi Eric

    Use the following query:

    select DATE,

    QUANTITY,

    DIVISION,

    LOCATION

    from TABLE

    where DATE in

    (

    select min(DATE) from TABLE

    group by to_char(DATE,'YYYY')

    )

    group by DATE,QUANTITY, DIVISION, LOCATION

    Hope this helps.

    Regards

    Nikhil

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Sep 18, 2008 at 11:20 PM

    Hi Eric,

    To get the earliest date values then insert a group on date field for each month and place all the fields in group header which gives you the first values (earliest dates) from your databsae fields. If you want to see thhe last dates then place all the fields in group footer,

    Make sure the group order should be in ascending.

    Regards,

    Raghavendra

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Raghavendra.

      Thanks for your suggestion but I'm not even close to putting my data in Crystal yet. This query is a small part of a much bigger query and I must have this puzzle the way it needs to be set. Otherwise, I will carry over a bunch of cartesians and duplicate data in to the main query.

      Thanks.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.