SELECT t.teamid, COUNT(CASE WHEN ashc.status = 'FAILED' THEN 1 END) AS failedcontrols,…
- User
SELECT t.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 INNER JOIN teams t ON aa.team_id = t.id GROUP BY t.team_id;
Will this SQL statement work?
- Assistant
Yes, this SQL statement should work as intended. It selects the team id, counts the number of controls with status 'FAILED' and 'PASSED' for each team, and groups the results by team id.
- User
Regarding the joins, can you give me the DB schema for this sql?
- Assistant
Sure! Here is the database schema for the SQL statement provided:
-
aws_securityhub_controlstable:- aws_account_id
- status
-
aws_accountstable:- id
- team_id
-
teamstable:- id
- team_id
The
aws_securityhub_controlstable is linked to theaws_accountstable through theaws_account_idforeign key. Theaws_accountstable is linked to theteamstable through theteam_idforeign key. This allows us to join the tables together in the SQL statement to retrieve the necessary information for counting the failed and passed controls for each team. -