Will this PostgreSQL statement work?: SELECT aa.tags->> 'ts-owner' AS teamid, COUNT(CASE…
- User
Will this PostgreSQL statement work?:
SELECT aa.tags->> 'ts-owner' AS team_id, COUNT(CASE WHEN ashc.status = 'FAILED' THEN 1 END) AS failed_controls, COUNT(CASE WHEN ashc.status = 'PASSED' THEN 1 END) AS passed_controls FROM aws_securityhub_controls ashc INNER JOIN aws_accounts aa ON ashc.aws_account_id = aa.id GROUP BY team_id;
- Assistant
The statement looks mostly correct, but there is a potential issue with the
GROUP BYclause. Theteam_idalias is being used in theSELECTclause, but it is not an actual column in theaws_securityhub_controlsoraws_accountstables. This means that theGROUP BYclause may not work as intended.To fix this issue, you can either repeat the
aa.tags->> 'ts-owner' AS team_idexpression in theGROUP BYclause, or you can use the same expression in theGROUP BYclause like this:sqlSELECT aa.tags->> 'ts-owner' AS team_id, COUNT(CASE WHEN ashc.status = 'FAILED' THEN 1 END) AS failed_controls, COUNT(CASE WHEN ashc.status = 'PASSED' THEN 1 END) AS passed_controls FROM aws_securityhub_controls ashc INNER JOIN aws_accounts aa ON ashc.aws_account_id = aa.id GROUP BY aa.tags->> 'ts-owner';This way, the query will properly group results by the
team_idalias.