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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Nasty sql... I'm stuck xpert help required!

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 intake
619 522 intake
619 529 intake
523 783 intake
523 781 intake

556 888 intake
556 822 intake
588 888 intake
588 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?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 ALONE
619 522 intake -- LEFT ALONE
619 529 intake -- LEFT ALONE
619 783 intake -- LEFT ALONE

523 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 ALONE
556 822 intake -- LEFT ALONE

588 888 intake -- DELETED (IT IS ALREADY IN base_group_id 556)
588 833 intake -- DELETED THEN MOVED TO 556 (IT ISN'T ALREADY THERE)
Go to Top of Page

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 Optimizer
TG
Go to Top of Page

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 both
delete mem
from sims.sims_member mem
where 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 kept
update mem set base_group_id = ttwm_keepint.intake_base_group_id
from sims.sims_member mem
inner join @temp_table_with_members ttwm
on ttwm.intake_base_group_id = mem.base_group_id
and ttwm.intake_to_keep is null -- this section gets all memberships where intake_to_keep is null
inner join @temp_table_with_members ttwm_keepint -- link to through to the group to keep with the pre-admision group
on ttwm_keepint.pread_base_group_id = ttwm.pread_base_group_id
and ttwm.intake_to_keep = 1
left outer join -- left join to correct memberships (see if they already exist)
(select distinct person_id, base_group_id
from sims.sims_member
) mem_keep
on mem_keep.base_group_id = ttwm_keepint.intake_base_group_id
and mem_keep.person_id = mem.person_id
where mem_keep.person_id is null
Go to Top of Page
   

- Advertisement -