Skip to content

Commit

Permalink
Feat: Add 311 example (#310)
Browse files Browse the repository at this point in the history
* 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
chellebodnar-google committed Mar 17, 2022
1 parent 63fc3a1 commit 844a7fb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions datasets/san_francisco_311/docs/queries/artifact.yaml
@@ -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
@@ -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;

0 comments on commit 844a7fb

Please sign in to comment.