Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Tim_Field
Yak Posting Veteran
87 Posts |
Posted - 2005-02-02 : 05:38:51
|
| Hi all,I'm getting lost in unions and joins here! Speed is not an issue with this script.....Thanks! I need to delete the year 15 from the table but first I need to replace the HIGHEST non year 15 end_date with the highest end_date that person has.person_id start_date end_date memberofgroup10762 2001-09-01 2004-07-21 1410762 2004-07-22 2004-09-21 1410762 2002-09-22 2005-01-01 1510111 2001-01-01 2002-12-31 1410111 2002-01-01 2003-12-31 1510111 2003-01-01 2004-12-31 14Should look like this, note for the second person the year 15 didn't have the highest end_date so is justdeleted.person_id start_date end_date memberofgroup10762 2001-09-01 2004-07-21 1410762 2004-07-22 2005-01-01 1410111 2001-01-01 2002-12-31 1410111 2003-01-01 2004-12-31 14 |
|
|
vganesh76
Yak Posting Veteran
64 Posts |
Posted - 2005-02-02 : 06:47:30
|
| declare @tbl table (person_id int,start_date datetime,end_date datetime ,memberofgroup int)insert into @tbl select 10762, cast('2001-09-01' as datetime),cast('2004-07-21' as datetime), 14 unionselect 10762, cast('2004-07-22' as datetime),cast('2004-09-21' as datetime), 14 unionselect 10762, cast('2002-09-22' as datetime),cast('2005-01-01' as datetime), 15 unionselect 10111, cast('2001-01-01' as datetime),cast('2002-12-31' as datetime), 14 unionselect 10111, cast('2002-01-01' as datetime),cast('2003-12-31' as datetime), 15 unionselect 10111, cast('2003-01-01' as datetime),cast('2004-12-31' as datetime), 14update @tbl set end_date = case when a.end_Date < (select max(end_date) from @tbl b where a.person_id=b.person_id and b.memberofgroup=15) then (select max(end_date) from @tbl b where a.person_id=b.person_id and b.memberofgroup=15) else a.end_Date end from @tbl a,(select person_id, max(end_date) end_date from @tbl where memberofgroup <> 15group by person_id ) updDate where a.person_id=upddate.person_id and a.end_date = upddate.end_date delete @tbl where memberofgroup=15select * from @tblEnjoy working |
 |
|
|
vganesh76
Yak Posting Veteran
64 Posts |
Posted - 2005-02-02 : 07:53:42
|
| You can also try this one,-----------------------declare @tbl table (person_id int,start_date datetime,end_date datetime ,memberofgroup int)insert into @tbl select 10762, cast('2001-09-01' as datetime),cast('2004-07-21' as datetime), 14 unionselect 10762, cast('2004-07-22' as datetime),cast('2004-09-21' as datetime), 14 unionselect 10762, cast('2002-09-22' as datetime),cast('2005-01-01' as datetime), 15 unionselect 10111, cast('2001-01-01' as datetime),cast('2002-12-31' as datetime), 14 unionselect 10111, cast('2002-01-01' as datetime),cast('2003-12-31' as datetime), 15 unionselect 10111, cast('2003-01-01' as datetime),cast('2004-12-31' as datetime), 14update @tbl set end_Date = (select max(end_Date) end_Date from @tbl c where c.person_id = a.person_id ) from @tbl a,(select person_id,max(end_Date) end_Date from @tbl where memberofgroup <> 15 group by person_id) updwhere a.person_id=upd.person_id and a.end_Date=upd.end_Datedelete @tbl where memberofgroup=15select * from @tblEnjoy working |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-02-02 : 08:03:28
|
| What about this?Declare @t table(person_id numeric,start_date smalldatetime,end_date smalldatetime,memberofgroup int)Declare @date1 smalldatetimeDeclare @date2 smalldatetimeinsert into @t select 10762, '2001-09-01 00:00:00', '2004-07-21 00:00:00', 14 union allselect 10762, '2004-07-22 00:00:00', '2004-09-21 00:00:00', 14 union allselect 10762, '2002-09-22 00:00:00', '2005-01-01 00:00:00', 15 union allselect 10111, '2001-01-01 00:00:00', '2002-12-31 00:00:00', 14 union allselect 10111, '2002-01-01 00:00:00', '2003-12-31 00:00:00', 15 union allselect 10111, '2003-01-01 00:00:00', '2004-12-31 00:00:00', 14select @date1=Max(End_date) from @t where memberofgroup=15select @date2=Max(End_date) from @t where memberofgroup=14 and person_id=(select max(person_id) from @t where memberofgroup=14)update @t set end_date=@Date1 where memberofgroup=14and end_date=@date2 delete from @t where memberofgroup=15Select * from @t Madhivanan |
 |
|
|
|
|
|
|
|