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-07-06 : 10:26:25
|
| Hi All, I'm a bit stuck can you help... anyone know how to display this properly? <code> doesn't seem to work.Thanks everyone! Tim.<code>/*pread_description intake_description pread_base_group_id intake_base_group_id intake_members intake_to_keep ----------------------- -------------------------- ------------------- -------------------- -------------- -------------- Nursery Intake 2005 Intake Group 2005-09-01A 45 619 1 1 Nursery Intake 2005 Intake Group 2005-09-01B 45 523 1 NULL Nursery Intake 2005 Intake Group 2005-09-01 45 511 NULL NULL Nursery Intake 2006 Intake Group 2006-09-01B 47 556 1 1 Nursery Intake 2006 Intake Group 2006-09-01 47 588 1 NULL */ /*member table -- Member of intake group (these must be moved!)base_group_id person_id GroupType-------------------- ----------- -------------619 751 intake619 522 intake619 529 intake523 783 intake523 781 intake556 888 intake556 822 intake588 888 intake588 833 intake*/</code>-- I need to move or remove members of those intake_base_group_id's (see top table) that are not flagged as intake_to_keep-- Therefore all members of intake_base_group_id 523 and 511 must be moved to 619 (moved if 619 does not already have a record otherwise deleted).-- Therefore all members of intake_base_group_id 588 must be moved to 556 (moved if 556 does not already have a record otherwise deleted).-- Can anyone help with the Sql!!? I'm stuck. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-07-06 : 10:33:38
|
| Can you post the expected result also?MadhivananFailing to plan is Planning to fail |
 |
|
|
Tim_Field
Yak Posting Veteran
87 Posts |
Posted - 2005-07-06 : 11:09:05
|
| Here you go! This is a bit more depth....member table -- Member of intake group (these must be moved!)First set of pread_base_group_id's (id = 45)base_group_id person_id GroupType-------------------- ----------- -------------619 751 intake -- LEFT ALONE619 522 intake -- LEFT ALONE619 529 intake -- LEFT ALONE619 783 intake -- LEFT ALONE523 783 intake -- DELETED (IT IS ALREADY IN base_group_id 619)523 781 intake -- DELETED THEN MOVED TO 619 (IT ISN'T ALREADY THERE)Next set of pread_base_group_id's (id = 47)base_group_id person_id GroupType--------------------------------------------556 888 intake -- LEFT ALONE556 822 intake -- LEFT ALONE588 888 intake -- DELETED (IT IS ALREADY IN base_group_id 556)588 833 intake -- DELETED THEN MOVED TO 556 (IT ISN'T ALREADY THERE) |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-07-06 : 13:53:48
|
quote: anyone know how to display this properly? <code> doesn't seem to work.
replace your <> with []for any code that will require a little trial and error, like your problem, please post the DDL/DML (script of table definition and script to insert sample data).Be One with the OptimizerTG |
 |
|
|
Tim_Field
Yak Posting Veteran
87 Posts |
Posted - 2005-07-07 : 10:21:54
|
I think I figured it out... nasty though!!! -- remove from member where they are in bothdelete mem from sims.sims_member memwhere mem.person_id IN ( -- Join to members to remove select mem_remove.person_id from sims.sims_member mem_remove inner join @temp_table_with_members ttwm on mem_remove.base_group_id = ttwm.intake_base_group_id and ttwm.intake_to_keep is null -- join to members to keep inner join sims.sims_member mem_keep on mem_keep.person_id = mem_remove.person_id inner join @temp_table_with_members ttwm2 on mem_keep.base_group_id = ttwm2.intake_base_group_id and ttwm2.intake_to_keep = 1 where ttwm.pread_base_group_id = ttwm2.pread_base_group_id -- pre-admission groups are the same. )and mem.base_group_id IN(select distinct intake_base_group_id from @temp_table_with_members ttwm where ttwm.intake_to_keep is null) -- find all unwanted intake groups-- update member where they are in the intake group we are removing, but not in the intake group that is being keptupdate mem set base_group_id = ttwm_keepint.intake_base_group_idfrom sims.sims_member meminner join @temp_table_with_members ttwmon ttwm.intake_base_group_id = mem.base_group_idand ttwm.intake_to_keep is null -- this section gets all memberships where intake_to_keep is nullinner join @temp_table_with_members ttwm_keepint -- link to through to the group to keep with the pre-admision groupon ttwm_keepint.pread_base_group_id = ttwm.pread_base_group_idand ttwm.intake_to_keep = 1left outer join -- left join to correct memberships (see if they already exist) (select distinct person_id, base_group_id from sims.sims_member ) mem_keepon mem_keep.base_group_id = ttwm_keepint.intake_base_group_id and mem_keep.person_id = mem.person_idwhere mem_keep.person_id is null |
 |
|
|
|
|
|