GROUP BY 在 BigQuery 中为什么会给我一个过高的行数?

huangapple go评论87阅读模式
英文:

Why does GROUP BY give me too high a row count in BigQuery

问题

I believe I am missing something (probably quite simple) in the use of GROUP BY in BigQuery, and I am hoping someone can set me straight.

比较这两个查询,我得到不同的用户数量

  1. SELECT SUM(users) FROM (
  2. SELECT
  3. DATE,
  4. COUNT(DISTINCT user_id) AS users,
  5. FROM
  6. `mytable`
  7. WHERE
  8. DATE BETWEEN ('2022-05-01') AND ('2022-05-31')
  9. GROUP BY
  10. DATE
  11. )
  12. users 的值约为:140,000
  13. SELECT
  14. COUNT(DISTINCT user_id) AS users,
  15. FROM
  16. `mytable`
  17. WHERE
  18. DATE BETWEEN ('2022-05-01') AND ('2022-05-31')
  19. users 的值约为:120,000
  20. <details>
  21. <summary>英文:</summary>
  22. I believe I am missing something (probably quite simple) in the use of **GROUP BY** in BigQuery, and I am hoping someone can set me straight.
  23. Comparing these two queries I get different numbers of users
  24. SELECT SUM(users) FROM (
  25. SELECT
  26. DATE,
  27. COUNT(DISTINCT user_id) AS users,
  28. FROM
  29. `mytable`
  30. WHERE
  31. DATE BETWEEN (&#39;2022-05-01&#39;) AND (&#39;2022-05-31&#39;)
  32. GROUP BY
  33. DATE
  34. )
  35. value for users approx: 140000
  36. SELECT
  37. COUNT(DISTINCT user_id) AS users,
  38. FROM
  39. `mytable`
  40. WHERE
  41. DATE BETWEEN (&#39;2022-05-01&#39;) AND (&#39;2022-05-31&#39;)
  42. value for users approx: 120000
  43. </details>
  44. # 答案1
  45. **得分**: 2
  46. 在第二个查询中,您正在计算整个日期范围内不同的 user_id 值的数量。在第一个查询中,您正在计算范围内 *每一天* 的不同 user_id 值,然后将它们相加。在第一个查询中,可能会统计不同日期上重复的用户。
  47. <details>
  48. <summary>英文:</summary>
  49. In the second query you&#39;re counting the distinct user_id values in the entire date range. In the first query you&#39;re counting the distinct user_id values *for each day* in the range, then summing those. There are probably duplicate users being counted on different days in the first query.
  50. </details>

huangapple
  • 本文由 发表于 2023年6月13日 01:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定