Selecting from two columns

  • Hi,

    I have a table with starttime and endtime

    i need to select the first starttime and the last endtime from the records. for example

    DECLARE@TBL TABLE (ID INT, StartDate DATETIME, EndDate DATETIME)

    INSERT INTO @TBL SELECT1,'2011-02-03 12:40:00.000', '2011-02-03 13:00:00.000'

    UNION SELECT1,'2011-02-03 14:00:00.000' ,'2011-02-03 14:50:00.000'

    UNION SELECT1,'2011-02-03 15:10:00.000','2011-02-03 16:10:00.000'

    UNION SELECT1,'2011-02-03 16:50:00.000','2011-02-03 17:10:00.000'

    from the above code i need output like

    id starttime endtime

    1 2011-02-03 12:40:00.000 2011-02-03 17:10:00.000

    how to achieve this?

    thanks

    Regards,

    Ami

  • need output like

    id starttime endtime

    1 2011-02-03 12:40:00.000 2011-02-03 17:10:00.000

    how to achieve this?

    select min(startdate) as startdate, MAX(enddate) as enddate from @TBL

    ----------
    Ashish

  • select Min(ID) AS ID, min(startdate) as starttime, MAX(enddate) as enddime from @TBL

  • Select id, min(StartDate), Max(EndDate) from @TBL

    group by id

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply