Same Date and Various Time Select in Single Row

  • EmpCodeShiftDateTime

    2058 01/02/2011 13:43

    2058 01/02/2011 20:12

    2058 01/03/2011 14:04

    2058 01/03/2011 20:13

    2058 01/04/2011 14:09

    2058 01/04/2011 20:12

    I Want Show Like This

    EmpCode ShiftDateTime TimeDetail

    2058 01/02/2011 13:43 13:43,2012

    2058 01/03/2011 14:04 14:04,20:13

    2058 01/04/2011 14:09 14:19,20:12

    Please Help me

  • You need to pivot the results:

    WITH SampleData

    AS (

    SELECT EmpCode,

    CAST(ShiftDateTime AS DateTime) AS ShiftDateTime,

    RN = ROW_NUMBER() OVER (PARTITION BY EmpCode, CAST(ShiftDateTime AS Date) ORDER BY ShiftDateTime)

    FROM (VALUES

    ('2058', '01/02/2011 13:43')

    ,('2058', '01/02/2011 20:12')

    ,('2058', '01/03/2011 14:04')

    ,('2058', '01/03/2011 20:13')

    ,('2058', '01/04/2011 14:09')

    ,('2058', '01/04/2011 20:12')

    ) AS Data (EmpCode,ShiftDateTime)

    )

    SELECT EmpCode, ShiftDate, [1], [2]

    FROM (

    SELECT EmpCode,

    CAST(ShiftDateTime AS Date) AS ShiftDate,

    CONVERT(char(5), ShiftDateTime, 114) AS ShiftTime,

    RN

    FROM SampleData

    ) AS src

    PIVOT( MIN(ShiftTime) FOR RN IN([1], [2]) ) AS pvt

    Hope this helps

    Gianluca

    -- Gianluca Sartori

  • Please keep to one thread

    http://www.sqlservercentral.com/Forums/Topic1169158-392-1.aspx



    Clear Sky SQL
    My Blog[/url]

  • Thanks

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

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