sql - MySql grouping returns incorrect result -
okay attempting sql magic sadly doesnt work expected. have following query:
select `u`.`id` `user_id`, `athm`.`academy_module_id` `module_id`, `ahat`.`academy_team_id` `team_id`, (case when ((`act`.`module_id` = `athm`.`academy_module_id`) , (`act`.`team_id` = `athm`.`academy_team_id`)) 1 else 0 end) `is_complete` (((`user` `u` join `user_has_academy_team` `ahat` on ((`ahat`.`user_id` = `u`.`id`))) join `academy_team_has_academy_module` `athm` on ((`ahat`.`academy_team_id` = `athm`.`academy_team_id`))) left outer join `academy_attempt` `act` on ((`act`.`module_id` = `athm`.`academy_module_id`)))
now without grouping returns following result:
user_id, module_id, team_id, is_complete '1', '11', '30', '1' '1', '11', '30', '1' '1', '11', '30', '1' '1', '11', '30', '1' '1', '11', '30', '1' '1', '11', '30', '1' '1', '11', '30', '1' '1', '11', '30', '1' '1', '11', '30', '1' '1', '12', '30', '1' '1', '12', '30', '1' '1', '12', '30', '1' '1', '12', '30', '1' '1', '5', '32', '0' '1', '10', '32', '0' '1', '12', '32', '0' '1', '12', '32', '0' '1', '12', '32', '0' '1', '12', '32', '0' '3', '5', '32', '0' '3', '10', '32', '0' '3', '12', '32', '0' '3', '12', '32', '0' '3', '12', '32', '0' '3', '12', '32', '0' '4', '5', '32', '0' '4', '10', '32', '0' '4', '12', '32', '0' '4', '12', '32', '0' '4', '12', '32', '0' '4', '12', '32', '0'
let me explain goal here:
when called module has been taking users in application attempt added academy_attempt
. user may take module several times i.e adding more rows academy_attempt
table. (as can see user 1 has taken of modules several times).
my goal find out modules has been taking , modules has not been taking.
sadly when group on academy_module_id
following result:
user_id, module_id, team_id, is_complete '1', '5', '32', '0' '1', '10', '32', '0' '1', '11', '30', '1' '1', '12', '30', '1'
i guessing because of left outer join
since other users not have record inacademy_attempt
result not shown.
my question is, there work around when working null data?
here table stuctures:
table: user_has_academy_team columns: user_id int(11) pk academy_team_id int(11) pk
table: academy_team_has_academy_module columns: academy_team_id int(11) pk academy_id int(11) pk academy_module_id int(11) pk
table: academy_attempt columns: id int(11) ai pk user_id int(11) academy_id int(11) module_id int(11) module_type_id int(11) team_id int(11) score int(11) medal_id int(11) pk timestamp datetime possible_correct int(11) user_correct int(11)
table: user columns: id int(11) ai pk username varchar(100) password varchar(100) is_active int(11) user_type_id int(11) token varchar(445) organization_id int(11) title_id int(11) image_path varchar(100) division_id int(11)
please let me know if need more information
Comments
Post a Comment