Prof. Falken
Prof. Falken

Reputation: 549

BigQuery Output the number of rows in a given query

I would like like to count the different types of an output after a query is run on BigQuery (legacy SQL). For example I have a table that has different types of html status codes. It looks like this when I run this query:

SELECT status, COUNT(status) FROM [app_logs] WHERE status >='300' AND DATE(CAST(start_time AS DATE)) >= '2017-11-23' AND DATE(CAST(end_time AS DATE)) <= '2017-12-22' GROUP BY status

Output:

Row status f0_
1 404 11
2 403 27
3 302 8
4 500 11

I would like the final output of the query to just count how many different status types (404, 403, 302, 500) there are, so in this example the final output would be:

f0_ 4

Is there a way to do this with just one query? Thanks in advance.

Upvotes: 0

Views: 1097

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 172993

#legacySQL
SELECT EXACT_COUNT_DISTINCT(status)
FROM [app_logs]
WHERE status >='300' 
AND DATE(CAST(start_time AS DATE)) >= '2017-11-23' 
AND DATE(CAST(end_time AS DATE)) <= '2017-12-22'

Upvotes: 1

Related Questions