| Author |
Topic |
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-13 : 13:23:27
|
| I have 1 table with a few columnstable name - users_tableid - user_name - group_resulttable name - groups_1id - user_name - group_nametable name - groups_2id - user_name - group_namei do a full outer join on groups_1 and groups_2 to figure out whats in sync.my question is, is there a way to fill in the group_result column, with data from the full outer join i did on the groups table?So for example, for each of the users in the users_table, it should get all ther egroups in groups_1 and groups_2, join them and if they all match fill in the group_result = 0 if something is not correct, or there is a non match somewhere, result = 1 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-13 : 13:26:03
|
| where's sample data and output?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-13 : 13:35:55
|
| Here is some sample datatable name - users_tableid - user_name - group_resultdata:1 - joe1 - null2 - joe2 - nulltable name - groups_1id - user_name - group_namedata:1 - joe1 - group 12 - joe1 - group 23 - joe2 - group 10table name - groups_2id - user_name - group_namedata:1 - joe1 - group 12 - joe1 - group 23 - joe2 - group 8So you see joe1 group all match if you did a join, and joe2 dont match so the result shoudl be 1...Users table should look like this after query updatedata:1 - joe1 - 02 - joe2 - 1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-13 : 13:39:39
|
| can group_1,group_2 etc contain duplicate sets with same value for user_name - group_name fields?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-13 : 13:41:55
|
| Yes so groups_1 and groups_2 could look like thistable name - groups_1id - user_name - group_namedata:1 - joe1 - group 12 - joe1 - group 23 - joe2 - group 104 - joe1 - group 1table name - groups_2id - user_name - group_namedata:1 - joe1 - group 12 - joe1 - group 23 - joe2 - group 84 - joe1 - group 1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-13 : 13:52:10
|
| [code][code]SELECT ut.ID,ut.user_name,CASE WHEN SUM(CASE WHEN Stat=1 THEN 1 ELSE 0 END) >0 THEN 1 ELSE 0 ENDFROM users_table utLEFT JOIN (SELECT user_name,group_name, CASE WHEN COUNT(DISTINCT cat) = 2 THEN 0 ELSE 1 END AS Stat FROM (SELECT user_name,group_name,'A' as cat FROM groups_1 UNION SELECT user_name,group_name,'B' as cat FROM groups_2 )t GROUP BY user_name,group_name )rON r.user_name = ut.user_nameGROUP BY ut.id,ut.user_name[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-13 : 21:50:26
|
| Where does it update the group_result column in the users_table? |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-14 : 10:44:12
|
| Now here a sample of my data, im not sure if this will work...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 1It should look for each record in the master_table, now some of the ad_username columns will be NULL so i dont want it to go crazy lol...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.joe1 - joe1 - 0 - 0joe2 - joe2 - 0 - 1joe4 - NULL - 1 - 0NULL - joe5 - 2 - 0########## Edit ###############33So i ran this belowSELECT ut.ad_username,CASE WHEN SUM(CASE WHEN Stat=1 THEN 1 ELSE 0 END) >0 THEN 1 ELSE 0 ENDFROM master_table utLEFT JOIN (SELECT student_id,student_group, CASE WHEN COUNT(DISTINCT cat) = 2 THEN 0 ELSE 1 END AS Stat FROM (SELECT student_id,student_group,'A' as cat FROM sis_students_groups UNION SELECT student_id,student_group,'B' as cat FROM ad_students_groups )t GROUP BY student_id,student_group )rON r.student_id = ut.ad_usernameGROUP BY ut.ad_usernameit only gives me a disply of ad_username, and a column with no name, i just dont see where its going to update the group_result column in the master_table based on the results...maybe i think i explained it wrong?? |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-14 : 12:36:50
|
| ? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-14 : 12:48:29
|
just turn the SELECT into an updateUPDATE utSET ut.group_result=m.resultFROM users_table utINNER JOIN (SELECT user_name,CASE WHEN SUM(CASE WHEN Stat=1 THEN 1 ELSE 0 END) >0 THEN 1 ELSE 0 END AS resultFROM(SELECT user_name,group_name, CASE WHEN COUNT(DISTINCT cat) = 2 THEN 0 ELSE 1 END AS Stat FROM (SELECT user_name,group_name,'A' as cat FROM groups_1 UNION SELECT user_name,group_name,'B' as cat FROM groups_2 )t GROUP BY user_name,group_name )rGROUP BY user_name)mON m.user_name = ut.user_name ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-14 : 13:40:24
|
| I cant seem to get that to work, did you see the new strucute above?UPDATE 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 Stat FROM (SELECT student_id,student_group,'A' as cat FROM ad_students_groups UNION SELECT student_id,student_group,'B' as cat FROM sis_students_groups )t GROUP BY student_id,student_group )rGROUP BY student_id)mON m.student_id = mt.ad_usernameerror messageMsg 207, Level 16, State 1, Line 5Invalid column name 'ad_username'.Msg 207, Level 16, State 1, Line 21Invalid column name 'student_id'. |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-14 : 15:17:41
|
| ? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-05-15 : 03:02:15
|
| please replace correct column names in query. i don't know what exactly are your column names (it seems to change with your every new post!). so make sure you replace correct ones in suggestion given. One thing i can see in your last query is you've used columns ad_username and column_id on outer query but haven't included them in inner queries.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-17 : 14:30:19
|
| I did replace the correct column names, but cant seem to findwhats wrongMy 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 1 |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-18 : 20:09:45
|
| anyone? |
 |
|
|
jjmusicpro
Yak Posting Veteran
79 Posts |
Posted - 2010-05-19 : 11:37:19
|
| Ive tried fixing my query by the table structre above, but still ant get it to work.Here 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 |
|
|
|