Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to retrieve this information?

Status
Not open for further replies.

vulc

Programmer
Apr 24, 2003
1
SD
Dear all,

I have following kind of table:

ID | StartTime | StopTime
------------------------------
1 | 9.00AM | 9.15AM
2 | 10.00AM | 11.00AM
3 | 10.10AM | 10.30AM
3 | 10.40AM | 10.50AM
4 | . | .
5 | . | .
5 | . | .
6 | |

I need to retrieve StartTime and StopTime for each ID. The problem is if there are two rows with the same ID, I need to get the StartTime from the first ID and StopTime from the last ID. Like for ID 3, the StartTime would be 10.0AM and stop Time 10.50AM.

Result needs to be like this:

ID | StartTime | StopTime
------------------------------
1 | 9.00AM | 9.15AM
2 | 10.00AM | 11.00AM
3 | 10.10AM | 10.50AM
4 | . | .
5 | . | .
5 | . | .
6 | |

I've been thinking of creating two different SQL queries, one for getting the ID&StartTime and the second one for getting ID&StopTime. Any ideas how to do this?

One solution could be code it on server side, but I think there might easy solution to fetch the information also with SQL sentences.
 
select id, min(starttime) starttime, max(stoptime) stoptime
from mytable
group by id
order by id
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top