Author |
Topic |
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2009-04-15 : 22:02:00
|
I have a complicated select from 10 tables. One of the tables has 3 columns. We are using temp tables and a query to get the data from 7 date columns into 3 columns. There are Date1 to Date7 columns which will have dates in any order. Now we have date1, date2 and date3 in the report which are suppose to have the 3 maximum values.If 2 values are same then they still get different rank and if all dates are same then 3 columns should get poplulated with same date.CREATE TABLE #temp(person_id int, date1 datetime,date2 datetime,date3 datetime,date4 datetime,date5 datetime,date6 datetime,date7 datetime)INSERT INTO #tempVALUES (111, '1/1/2009',null,null,null,'1/1/2009','1/1/2009','1/1/2009')INSERT INTO #tempVALUES (222, '1/1/2007',null,null,null,'1/1/2008','1/1/2009','1/1/2009')INSERT INTO #tempVALUES (333, null,'2/2/2007','3/3/3006',null,'1/1/2008','1/1/2008','1/1/2009')INSERT INTO #tempVALUES (444, '2/1/2009',null,null,null,'4/1/2009','3/1/2009','1/1/2009')So the result we should get isPERSON_ID, date1, date2, date3111,'1/1/2009','1/1/2009','1/1/2009'222,'1/1/2008','1/1/2009','1/1/2009'333,'1/1/2008','1/1/2008','1/1/2009'444,'2/1/2009','3/1/2009','4/1/2009'There is no specific order in which 7 dates are populated and temp tables or table variable are crashing our systems. due to some memory issues. Can this be done wihout temp tables.Each person should have only 1 row.Help appreciated. THANKS-----------------------------------------------------------------------------------------------Ashley Rhodes |
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-04-15 : 22:37:17
|
is this u want check it once.select person_id,max(isnull(date1,date7)),max(isnull(date6,date2)),max(isnull(date5,date3)) from #temp group by person_iddrop table #temp |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2009-04-16 : 10:25:30
|
no this will not workall 3 columns should have different values how will you tell with this query-----------------------------------------------------------------------------------------------Ashley Rhodes |
|
|
dsindo
Starting Member
45 Posts |
Posted - 2009-04-16 : 15:50:30
|
if object_id('tempdb..#temp') is not null DROP TABLE #tempCREATE TABLE #temp(person_id int, date1 datetime,date2 datetime,date3 datetime,date4 datetime,date5 datetime,date6 datetime,date7 datetime)INSERT INTO #tempVALUES (111, '1/1/2009',null,null,null,'1/1/2009','1/1/2009','1/1/2009')INSERT INTO #tempVALUES (222, '1/1/2007',null,null,null,'1/1/2008','1/1/2009','1/1/2009')INSERT INTO #tempVALUES (333, null,'2/2/2007','3/3/2006',null,'1/1/2008','1/1/2008','1/1/2009')INSERT INTO #tempVALUES (444, '2/1/2009',null,null,null,'4/1/2009','3/1/2009','1/1/2009')if object_id('tempdb..#temp2') is not null drop table #temp2create table #temp2 (per_id int,datex datetime)insert into #temp2select person_id, date1 from #tempunion all select person_id, date2 from #tempunion all select person_id, date3 from #tempunion all select person_id, date4 from #tempunion all select person_id, date5 from #tempunion all select person_id, date6 from #tempunion all select person_id, date7 from #tempdrop table #temp3create table #temp3 (id int identity(1,1),per_id int)insert into #temp3select distinct person_id from #tempdeclare @col int, @cols int,@i int,@y int select @col=min(id), @cols=max(id) from #temp3if object_id('tempdb..#temp6') is not null drop table #temp6create table #temp6 (x_id int , id int, per_id int, date1 datetime)while @cols >= @col begin drop table #temp5 create table #temp5 (x_id int identity(1,1), id int, per_id int, date1 datetime) insert into #temp5 select top 3 id, a.per_id,datex from #temp2 a inner join #temp3 b on a.per_id=b.per_id where datex is not null and id = @col order by id,datex desc insert into #temp6 select * from #temp5 set @col=@col+1 end select per_id, max(case when x_id=3 then date1 else null end) as date1, max(case when x_id=2 then date1 else null end) as date2, max(case when x_id=1 then date1 else null end) as date3 from #temp6 group by per_id |
|
|
ashley.sql
Constraint Violating Yak Guru
299 Posts |
Posted - 2009-04-17 : 14:26:01
|
HiYou are using TEMP tablesI am already doing this with TEMP TABLES.Can not use temp tables due to memory issues-----------------------------------------------------------------------------------------------Ashley Rhodes |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-17 : 15:31:48
|
SELECT person_id,dateval INTO Unpivotted_TableFROM(SELECT person_id,date1 AS datevalFROM yourtableWHERE date1 IS NOT NULLUNION ALLSELECT person_id,date2FROM yourtableWHERE date2 IS NOT NULLUNION ALLSELECT person_id,date3FROM yourtableWHERE date3 IS NOT NULL...SELECT person_id,date7FROM yourtableWHERE date7 IS NOT NULL)tSELECT u.person_id,(SELECT TOP 1 dateval FROM(SELECT TOP 3 dateval FROM Unpivotted_Table WHERE person_id=u.person_id ORDER BY dateval DESC)p ORDER BY dateval) AS date1,(SELECT TOP 1 dateval FROM(SELECT TOP 2 dateval FROM Unpivotted_Table WHERE person_id=u.person_id ORDER BY dateval DESC)p ORDER BY dateval) AS date2,(SELECT TOP 1 dateval FROM Unpivotted_Table WHERE person_id=u.person_id ORDER BY dateval DESC) AS date3FROM Unpivotted_Table uDROP TABLE Unpivotted_Table |
|
|
|
|
|