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-05-19 : 12:40:52
|
| I cant seem to get this update correct.I have the table stucture like below. It should look at ad_username in the master_table, check that user id in both sis_students_groups and ad_students_groups tables, to see if they all match, or if one is missing from either table. If the items do not match, then group_result should = 1, if they do match group_result should = 0.My master_table (pushed in from full outer join)structre likead_username - username - join result - group_resultdata examplejoe1 - joe1 - 0 - 0joe2 - joe2 - 0 - 0joe4 - NULL - 1 - 0NULL - joe5 - 2 - 0table name - sis_students_groupsid - student_id - student_groupdata:1 - joe1 - group 12 - joe1 - group 23 - joe2 - group 104 - joe1 - group 1table name - ad_students_groupsid - student_id - student_groupdata:1 - joe1 - group 12 - joe1 - group 23 - joe2 - group 84 - joe1 - group 1UPDATE mtSET mt.group_result=m.resultFROM master_table mtINNER JOIN (SELECT ad_username,CASE WHEN SUM(CASE WHEN Stat=1 THEN 1 ELSE 0 END) >0 THEN 1 ELSE 0 END AS resultFROM(SELECT student_id,student_group,CASE WHEN COUNT(DISTINCT cat) = 2 THEN 0 ELSE 1 END AS StatFROM(SELECT student_id,student_group,'A' as catFROM ad_students_groupsUNION SELECT student_id,student_group,'B' as catFROM sis_students_groups)tGROUP BY student_id,student_group)rGROUP BY student_id)mON m.student_id = mt.ad_usernameHere is the error:Msg 207, Level 16, State 1, Line 5Invalid column name 'ad_username'.Msg 207, Level 16, State 1, Line 21Invalid column name 'student_id'. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-19 : 12:57:49
|
[code]UPDATE mtSET mt.group_result=m.resultFROM master_table mtINNER JOIN (SELECT ad_usernamestudent_id,CASE WHEN SUM(CASE WHEN Stat=1 THEN 1 ELSE 0 END) >0 THEN 1 ELSE 0 END AS resultFROM(SELECT student_id,student_group,CASE WHEN COUNT(DISTINCT cat) = 2 THEN 0 ELSE 1 END AS StatFROM(SELECT student_id,student_group,'A' as catFROM ad_students_groupsUNION SELECT student_id,student_group,'B' as catFROM sis_students_groups)tGROUP BY student_id,student_group)rGROUP BY student_id)mON m.student_id = mt.ad_username[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-19 : 13:02:43
|
| THANKS!!!!!!! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-19 : 13:05:47
|
| welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|