I'm looking to recategorise some relatively simple information in the most efficient way possible: Using a limited selection of sample data:
CREATE TABLE #data
(id varchar(30)
,payent_type varchar(30)
,payment_date DATETIME)
INSERT INTO #data values ('001','single gift',DATEADD(MM,-12,GETDATE()))
INSERT INTO #data values ('001','regular gift',DATEADD(MM,-39,GETDATE()))
INSERT INTO #data values ('002','regular gift',DATEADD(MM,-06,GETDATE()))
INSERT INTO #data values ('003','single gift',DATEADD(MM,-96,GETDATE()))
INSERT INTO #data values ('003','regular gift',DATEADD(MM,-96,GETDATE()))
INSERT INTO #data values ('003','single gift',DATEADD(MM,-1,GETDATE()))
INSERT INTO #data values ('004','single gift',DATEADD(MM,-54,GETDATE()))
INSERT INTO #data values ('005','regular gift',DATEADD(MM,-2,GETDATE()))
INSERT INTO #data values ('005','regular gift',DATEADD(MM,-8,GETDATE()))
INSERT INTO #data values ('006','single gift',DATEADD(MM,-12,GETDATE()))
INSERT INTO #data values ('007','regular gift',DATEADD(MM,-2,GETDATE()))
INSERT INTO #data values ('007','regular gift',DATEADD(MM,-6,GETDATE()))
INSERT INTO #data values ('008','single gift',DATEADD(MM,-1,GETDATE()))
INSERT INTO #data values ('009','single gift',DATEADD(MM,-80,GETDATE()))
INSERT INTO #data values ('010','single gift',DATEADD(MM,-54,GETDATE()))
And turning it into this:
ID | 2005 | 2006 | 2007 | 2008 | 2009 | 2010
001 | NULL | NULL | regular gift | NULL | Both gifts | NULL
Where basically if ID has both a single and a regular gift for a year then call it 'both gifts', if there is only a single gift then 'single gift' and if there is only a regular gift then 'regular gift'.
This data would then be used as part of another larger query.
I'm finding it easy enough to work out if an ID ever gave a single or regular (or both) gift, but I'm struggling to work it out on a year-by-year basis without creating a series of temporary tables which slows things down massively
Thanks in advance :)
EDIT This is a very simplified version of the actual data I have - the real data has up to 200 rows per ID, and can contain multiple gifts per year of each type.