I have this SQL data structure: - Table awssecurityhubcontrols with columns arn, status…
- User
I have this SQL data structure:
- Table aws_securityhub_controls with columns arn, status (FAILED or PASSED) and aws_account_id (references aws_accounts table id column)
- Table aws_accounts with columns id and team_id
Write me a SQL query to get the number of failed and passed controls grouped by the team id
- Assistant
You can use a combination of Join, Group By, and Count functions to achieve this. Here's an example SQL query to get the number of failed and passed controls grouped by the team id:
SELECT aa.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 JOIN aws_accounts aa ON ashc.aws_account_id = aa.id GROUP BY aa.team_id;In this query, we are joining the
aws_securityhub_controlstable with theaws_accountstable using theaws_account_idandidcolumns respectively. Then, we are grouping the results by theteam_idcolumn.Inside the
COUNTfunction, we are using aCASEstatement to count only the records with a specific control status (either 'FAILED' or 'PASSED'). TheCASEstatement returns 1 for the matching status and NULL for other statuses. TheCOUNTfunction only counts non-NULL values, so it will only count the matching statuses.Finally, the query returns the team_id along with the counts of failed_controls and passed_controls for each team.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.