Please fix the incorrect catches count from the below query. It seems due to some…
- User
Please fix the incorrect catches count from the below query. It seems due to some scorecard_id duplcates causing that involved innings_batting, scorecard and scorecard_match_type tables.
SELECT p.work_player_id, ibat2.catches, ibat2.wkcatches, ibat2.nonwkcatches, ibat2.stumpings FROM player p LEFT OUTER JOIN ( SELECT ifield.work_fielder_id, COALESCE(SUM(IF(ifield.how_out = 'c' AND ifield.fielder_is_substitute != 'y', 1, 0)), 0) AS catches, COALESCE(SUM(IF(ifield.how_out = 'st', 1, 0)), 0) AS stumpings, COALESCE(SUM(IF(ifield.how_out = 'c' AND ifield.fielder_is_substitute != 'y' AND ifield.fielder_wicket_keeper IN ('y', '+'), 1, 0)), 0) AS wkcatches, COALESCE(SUM(IF(ifield.how_out = 'c' AND ifield.fielder_is_substitute != 'y' AND ifield.fielder_wicket_keeper IN ('n', '-'), 1, 0)), 0) AS nonwkcatches FROM innings_batting ifield JOIN scorecard sc ON ifield.scorecard_id = sc.scorecard_id JOIN scorecard_match_type smt ON sc.scorecard_id = smt.scorecard_id WHERE ifield.work_fielder_id = 402600 AND smt.match_type = 'tt' GROUP BY ifield.work_fielder_id ) ibat2 ON p.work_player_id = ibat2.work_fielder_id WHERE p.work_player_id = 402600;
- Assistant
It looks like the issue lies in the potential duplicates due to the way scorecard IDs are joining with
innings_batting,scorecard, andscorecard_match_typetables. To fix this, we can enforce a distinct pair ofscorecard_idandwork_fielder_idbefore performing our calculations by using a subquery or common table expression (CTE).Here's the revised query to address the duplicate issue: