Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create category_by_complaint_source.sql Add a query snippet * Create artifact.yaml * Improve formatting * Add better comments Add comments and change variable "num_instances" to "num_compaints" for clarity
- Loading branch information
1 parent
63fc3a1
commit 844a7fb
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
artifact: | ||
title: Analyze most prevelant category by complaint source of issue | ||
description: In this tutorial we analyze the most likely category corresponding to each complaint source from all 311 reports in San Francisco. | ||
vertical: government | ||
tier: free |
30 changes: 30 additions & 0 deletions
30
..._francisco_311/docs/queries/category_by_complaint_source/category_by_complaint_source.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# What is the most common category for each complaint source? | ||
|
||
WITH source_category_counts AS ( | ||
SELECT | ||
source, | ||
category, | ||
COUNT(1) AS num_complaints | ||
FROM | ||
`bigquery-public-data`.san_francisco_311.311_service_requests | ||
GROUP BY | ||
source, category | ||
) | ||
SELECT | ||
source, | ||
category, | ||
num_complaints, | ||
num_complaints/total AS fraction_of_source | ||
FROM | ||
(SELECT | ||
source, | ||
category, | ||
num_complaints, | ||
# Within each source, rank the categories by number of complaints in descending order. | ||
ROW_NUMBER() OVER (PARTITION BY source ORDER BY num_complaints DESC) AS rank, | ||
# Compute the total number of complaints reported per source | ||
SUM(num_complaints) OVER (PARTITION BY source) total | ||
FROM source_category_counts) | ||
WHERE | ||
# Extract the most common category of complaint | ||
rank = 1; |