Skip to content

Commit

Permalink
Add query for getting the daily totals from Athena.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarno Rantanen committed Apr 24, 2020
1 parent 321885f commit d915503
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/backend/queries.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { flatten } from 'lodash';
import { stringLiteralUnionFields } from '../common/model';

export const totalResponsesQuery = 'SELECT COUNT(*) as total_responses FROM responses';

export const postalCodeLevelDataQuery = `SELECT postal_code,
Expand Down Expand Up @@ -34,3 +37,32 @@ export const postalCodeLevelDataQuery = `SELECT postal_code,
FROM responses WHERE (country_code = 'FI' or country_code = '')
GROUP BY postal_code
ORDER BY responses DESC`;

export const dailyTotalsQuery = `
SELECT
day,
COUNT(*) AS total,
${flatten(
stringLiteralUnionFields.map(([field, values]) =>
values.map(value => `SUM(${field}_${value}) AS ${field}_${value}`),
),
).join(',\n')}
FROM
(
SELECT
SUBSTR(timestamp, 1, 10) AS day,
${flatten(
stringLiteralUnionFields.map(([field, values]) =>
values.map(value => `IF(${field} = '${value}', 1, 0) AS ${field}_${value}`),
),
).join(',\n')}
FROM
responses
WHERE
country_code = 'FI'
OR
country_code = ''
)
GROUP BY day
ORDER BY day
`;

0 comments on commit d915503

Please sign in to comment.