1

I have 3 columns in my DB table as follows. 1. START_TIME 2. END_TIME 3. INDEX_NO

Sample records below

INDEX_NO                END_TIME                        START_TIME                      
------------- ------------------------------- -------------------------------
I152         13-JAN-14 02.30.00.000000000 AM 13-JAN-14 12.10.00.000000000 AM 
I151         13-JAN-14 05.30.00.000000000 AM 13-JAN-14 03.15.00.000000000 AM 
I152         20-JAN-14 02.30.00.000000000 AM 20-JAN-14 12.10.00.000000000 AM 
I151         20-JAN-14 05.30.00.000000000 AM 20-JAN-14 03.15.00.000000000 AM  

From the above result set i need extract the MIN(START_TIME) and MAX(END_TIME) along with the INDEX_NO of the record with MIN(START_TIME)

To summarize the expected output is as follows.

INDEX_NO                END_TIME                        START_TIME                      
    ------------- ------------------------------- -------------------------------
    I152         20-JAN-14 05.30.00.000000000 AM 13-JAN-14 12.10.00.000000000 AM 

Can anyone help me with a query of minimal cost to do the above extraction?

i am using oracle 11g.

2 Answers 2

3

FIRST function will come handy here.

select min(index_no) keep(dense_rank first order by start_time),
       max(end_time),
       min(start_time)
from mytab;

Demo here.

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, every time I see queries like this I tell myself I need to read and practise the aggregate functions.
-1

You start by writing the inner query, which finds the min time and the max time. You then write a query that selects the correct fields from the table, join it against the inner query with the min_time field.

This will produce multiple rows if there are several rows with the same minimum start time.

SELECT 
    mt.INDEX_NO,
    mt.START_TIME,
    t."max_time"
FROM MyTable mt
JOIN
(
SELECT
    min(START_TIME) as "min_time",
    max(END_TIME) as "max_time"
FROM MyTable
) t
ON mt.START_TIME = t."min_time"
;

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.