Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Taking rows RegID Date (Registration with multiple dates) and changing format to columns) Expand / Collapse
Author
Message
Posted Wednesday, February 18, 2009 9:46 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, August 15, 2013 7:58 AM
Points: 423, Visits: 635
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.

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 Date2 Date3 Date4
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
Post #659571
Posted Tuesday, March 17, 2009 4:05 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, November 6, 2014 5:01 AM
Points: 2,116, Visits: 6,441
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 . 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)
Post #677964
Posted Wednesday, March 18, 2009 3:14 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Thursday, August 15, 2013 7:58 AM
Points: 423, Visits: 635
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
Post #678205
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse