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 |
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-07-19 : 12:59:55
|
| I cant seem to figure this query out.I have 3 tables, I want to loop through all the id's, and update a column in the main table, with query results from 2 other tables.master_table_staffid - username - userid - group_resultExample Data1 - joe1 - 11111 - some_group - 02 - joe2 - 11112 - some_group_2 - 0ad_staff_groupsid - employee_id - employee_groupExample Data1 - 11111 - some_group2 - 11111 - some_group_1hr_staff_groupsid - employee_id - employee_groupExample Data1 - 11111 - some_groupResults Table Should look, and see that id 11111 has "some_group" in both tables, but some_group_1 is not in both tables, so the result should be 1.If all groups match in both tables, then the group_result should be a 0, if there are any non matching groups, the result should be a 1.AFTER QUERYmaster_table_staffid - username - userid - group_resultExample Data1 - joe1 - 11111 - some_group - 12 - joe2 - 11112 - some_group_2 - 0Here is what I had, but cant figure it out.UPDATE mtSET mt.group_result=m.resultFROM master_table_staff mtINNER JOIN (SELECT employee_id,CASE WHEN SUM(CASE WHEN Stat=1 THEN 1 ELSE 0 END) >0 THEN 1 ELSE 0 END AS resultFROM(SELECT employee_id,employee_group,CASE WHEN COUNT(DISTINCT cat) = 2 THEN 0 ELSE 1 END AS StatFROM(SELECT employee_id,employee_group,'A' as catFROM ad_staff_groupsUNION SELECT employee_id,employee_group,'B' as catFROM hr_staff_groups)tGROUP BY employee_id,employee_group)rGROUP BY employee_id)mON m.employee_id = mt.ad_staff_id |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-07-20 : 06:44:22
|
| I think first table structure have one missing column Employee group. Please tell me my guess is correct or not so that i can think more on requirement.Vaibhav TTo walk FAST walk ALONE To walk FAR walk TOGETHER |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-07-20 : 09:30:49
|
| anyone ? |
 |
|
|
|
|
|
|
|