Select activity from a specific date using SQL

寵の児 提交于 2019-12-25 01:18:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!