2

I want to select the earliest date and time from my data set and only show those row(s) that fit the requirement. And show 3 columns.

I got it to show the data in the right order by date and time. How can I just get it to show the data that have the mininum values? I tried using first, limit, and top x, but they don't work, and aren't exactly what I need since the answer may have more than 1 value.

Here is my example sql:

Select report, date, time
  From events
 order by date, time
5
  • What is the data type of DATE and TIME? DATE is a reserved word so I assume that is not actually your column name. If the column is defined as data type DATE, it will have a time component so I'm not sure why there would be a separate TIME column or how the two times are related. For each report, do you want to pick the row with the minimum date and time? Or do you want to globally pick the row with the minimum date and time? Feb 29, 2012 at 22:38
  • I'm not using the word date and time. I just changed it to be simple. The date and times are not together. (Date and time are not in the standard format; they are listed as 1/1/2012 and military time 2212. I want to pick the row (s) with the mininum date and time for each report. Feb 29, 2012 at 22:42
  • 1
    What is the data type of the two columns? Are these DATE columns? VARCHAR2? TIMESTAMP? Something else? Feb 29, 2012 at 22:49
  • Both the date and time column are varchar2. Feb 29, 2012 at 22:52
  • 1
    OK. And the day portion, is that stored in MM/DD/YYYY or DD/MM/YYYY format? Feb 29, 2012 at 22:53

3 Answers 3

4

Try this:

 SELECT report, date, time
 FROM (SELECT report, date, time,
         ROW_NUMBER() OVER(PARTITION BY report ORDER BY date ASC, time ASC) AS RowNum
       From events
      ) AS CTE 
 WHERE CTE.RowNum = 1 
2
  • This code doesn't work at all. I'm getting an error at the first RowNum. Mar 1, 2012 at 0:26
  • rownum is a pseudo column name, so you can't use it as an alias here; some other name (like RN) would work. You also can't use AS for the table alias in Oracle, so the fifth line would just be ) CTE, though a name isn't really necessary at all in this case. And the ordering by date isn't be right because it's a varchar, so it would put Jan 1 2012 before Dec 31 2011.
    – Alex Poole
    Mar 1, 2012 at 8:36
1

Something like this should work assuming that every row has a validly formatted day and time component.

SELECT report,
       dt,
       time
  FROM (SELECT report,
               dt,
               time,
               rank() over (partition by report
                                order by to_date( dt || ' ' || time, 'MM/DD/YYYY HH24MI' ) asc) rnk
          FROM events)
 WHERE rnk = 1

From a data model standpoint, however, you should always store dates in DATE columns rather than trying to store them in VARCHAR2 columns. Since you want date comparison and sorting semantics, you'll have to transform the data into a DATE which is costly at runtime. And there is a great chance that someone will eventually store data in a different format in the column or store an invalid string (i.e. a day of '02/29/2011') which will cause your query to start generating errors.

2
  • 1
    +1 both for going straight for rank, and for saving me from typing a diatribe about not storing dates in date columns.
    – Alex Poole
    Feb 29, 2012 at 23:04
  • Next time, the date/time data will be stored in the correct format. This code just results in showing all of my results in the order that they were inside of the text file. Mar 1, 2012 at 0:25
1

Guessing a bit as the data types aren't clear, but something like this might work (example using a CTE to generate dummy data):

with events as (
select 'report1' as report, '01/01/2012' as date_field, '0800' as time_field
    from dual
union all select 'report1', '01/01/2012', '0900' from dual
union all select 'report1', '01/02/2012', '0930' from dual
union all select 'report2', '01/01/2012', '0900' from dual
union all select 'report2', '01/01/2012', '0900' from dual
union all select 'report2', '01/01/2012', '1000' from dual
)
select report, date_field, time_field
from (
    select report, date_field, time_field,
        row_number() over (partition by report
            order by to_date(date_field, 'MM/DD/YYYY'), time_field) as rn
    from events
)
where rn = 1
order by report;

REPORT  DATE_FIELD TIME
------- ---------- ----
report1 01/01/2012 0800
report2 01/01/2012 0900

You may have a different date format mask; I've assumed US format as you referred to 'military time'.

Depending on how you want to treat ties, you'll want rank or dense_rank instead of row_number. See the documentation of analytic functions for more info. As Justin pointed out you probably want rank, which with the same data gives:

REPORT  DATE_FIELD TIME
------- ---------- ----
report1 01/01/2012 0800
report2 01/01/2012 0900
report2 01/01/2012 0900

The inner select adds an extra rn column that assigns a ranking to each result; each value of report will have at least one row that gets assigned 1 (if using rank, otherwise exactly one), and possibly rows with 2, 3 etc. The one(s) with 1 will have the earliest date/time for that report. The outer query then filters to only show those ranked 1, via the where rn = 1 clause, hence only giving the data with the earliest date/time for each report - the rest is discarded.

4
  • Since it sounds like he wants to get multiple rows if there is a tie, I'd expect that he'd want the RANK or DENSE_RANK analytic function rather than ROW_NUMBER. Feb 29, 2012 at 22:57
  • @Justin Cave - I missed he'd specified that; I just added my usual caveat, as I seem to default to row_number.
    – Alex Poole
    Feb 29, 2012 at 23:02
  • This is the only code that works so far, but I'm still getting the same result as my original SQL. It is showing all of the rows in the results, instead of just the mininum value(s). I am a "she" by the way. Mar 1, 2012 at 0:21
  • @user1094628 - Sorry, no offence meant *8-) This should show one row per report; with rank it could show more than one. I've updated the answer with sample data and output. If that's not what you're getting, we must be missing something you've simplified or misunderstood what you want, in which case can you update the question to some actual sample data and what you want the output to be?
    – Alex Poole
    Mar 1, 2012 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.