Taking rows RegID Date (Registration with multiple dates) and changing format to columns)

  • Hi

    I have tried and failed many times now to try and get my head round this problem but its cropped up again and I could really do with some help.

    :blink:

    I have a registration (2 in the example) and they each have a number of conferences against them.

    The example data is as follows

    /****** Object: Table [dbo].[ECAFALL] Script Date: 18/02/2009 11:10:05 ******/

    CREATE TABLE [dbo].[Registration_Conference_Dates](

    [Registration_ID] [int] NULL,

    [Conference_Actual_Date] [datetime] NULL,

    [Number of Conferences on date] [Int]

    ) ON [PRIMARY]

    INSERT INTO [dbo].[ECAFALL]

    (Person, Type, [ID], START_DATE, END_DATE)

    SELECT 12054,'ECAFSTD',496815,'2007-08-29 13:37:35.923','2007-08-29 13:38:15.357'

    UNION

    SELECT 17039,'2008-07-15 00:00:00.000',3

    UNION

    SELECT 17039,'2008-12-04 00:00:00.000',2

    UNION

    SELECT 17039,'2009-02-13 00:00:00.000',3

    UNION

    SELECT 18867,'2005-12-21 00:00:00.000',2

    UNION

    SELECT 18867,'2006-03-07 00:00:00.000',2

    UNION

    SELECT 18867,'2006-07-11 00:00:00.000',7

    UNION

    SELECT 18867,'2006-12-13 00:00:00.000',5

    How do I get the above into this format.......

    Registration_ID Date1 Date2Date3Date4

    17039'2008-07-15 00:00:00.000' '2008-12-04 00:00:00.000' '2009-02-13 00:00:00.000'

    18867'2005-12-21 00:00:00.000' '2006-03-07 00:00:00.000' '2006-07-11 00:00:00.000''2006-12-13 00:00:00.000'

    The Registrations can have up to 30 dates attatched. This is intended to create just one row of data for each registration showing the entire life of the process

  • Hi Debbie,

    You CAN do this in T-SQL if you REALLY want to, but it would probably involve using cursors, yuk, so we won't want to go there, and in fact I don't even want to think how to do it. Jeff would hate me for giving you such a solution in the first place :-D. Anyhow, this is not something you want to do on the server, it should be done in your front-end. Just get the data out in the right order, then assign it to the Date1, Date2 etc. column in the output grid or wherever you want to present the data.

    And by the way, your CREATE TABLE statement doesn't match the table your sample data refers to. But that's an aside.

    Greets,

    Jan

    --------------------------------------------------------------------------
    A little knowledge is a dangerous thing (Alexander Pope)
    In order for us to help you as efficiently as possible, please read this before posting (courtesy of Jeff Moden)[/url]

  • Whoops sorry about that.

    I used an old create table script and didnt finish it properly.

    Thanks for the advice. I think I used MIN, MAX and then grabbed the other ID that isnt in the min OR MAX temporaty tables.

    Debbie

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

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