问题
I want to look up the number of questions are asked in a specific day on the Stack Overflow Question and answer dataset. How many questions were asked at 2018-11-11?
how = """SELECT
EXTRACT(DAY FROM DATE '2018-11-11') AS Day,
EXTRACT(MONTH FROM DATE '2018-11-11') AS Month,
EXTRACT(YEAR FROM DATE '2018-11-11') AS Year,
COUNT(*) AS Number_of_Questions,
ROUND(100 * SUM(IF(answer_count > 0, 1, 0)) / COUNT(*), 1) AS Percent_Questions_with_Answers
FROM
`bigquery-public-data.stackoverflow.posts_questions`
GROUP BY
Day
HAVING
Day > 0 AND day < 12
ORDER BY
Day;
"""
how = stackOverflow.query_to_pandas_safe(how)
how.head(12)
The code I use retrieves back all questions asked in the whole dataset Instead on the date I have selected. If I try to filter with @@ I get an error
回答1:
Wouldn't the query look like this?
SELECT COUNT(*) AS Number_of_Questions
FROM `bigquery-public-data.stackoverflow.posts_questions`
WHERE DATE = DATE('2018-11-11');
EDIT:
I see this is a public data set. Assuming you mean the creation date, then:
SELECT count(*)
FROM `bigquery-public-data.stackoverflow.posts_questions` pq
WHERE creation_date >= TIMESTAMP('2018-11-11') and
creation_date < TIMESTAMP('2018-11-12') ;
This code is tested and works when I run it.
回答2:
Below is for BigQuery Standard SQL
#standardSQL
SELECT
DATE(creation_date) AS day,
COUNT(*) AS Number_of_Questions,
ROUND(100 * COUNTIF(answer_count > 0) / COUNT(*), 1) AS Percent_Questions_with_Answers
FROM `bigquery-public-data.stackoverflow.posts_questions`
WHERE DATE(creation_date) BETWEEN '2018-11-01' AND '2018-11-11'
GROUP BY day
-- ORDER BY day
with result
来源:https://stackoverflow.com/questions/55457277/select-activity-from-a-specific-date-using-sql