BigQueryテクニック集 – 欠落日付の補完

SQL

and,a株式会社 デジタルマーケティングエンジニア 佐藤洋太

GA4のSQLを開発で日付の出力の際にこんな経験はありませんか?
「日付が歯抜けで抽出されている?」
「飛び飛びで日付が生成されていて綺麗に1日おきにデータがない」
今回はデータ分析時における欠落日付の補完のお悩みを解決したいと思います。

準備

ごく簡単な例です。
まずはこのようなテーブルがあるとしましょう。

dateurlcount
12022-09-01https://www.example.com/page01/2
22022-09-02https://www.example.com/page01/3
32022-09-03https://www.example.com/page01/4
42022-09-05https://www.example.com/page01/6
52022-09-07https://www.example.com/page01/8

内容はpage01の日付ごとのページビュー数(ないしイベント数)とします。
注目するポイントは2022/09/04と2022/09/06のレコードがない点です。
このような欠落日付のレコードは分析時に取り回しが非常に悪いです。

では、2022/09/04と2022/09/06にcountが0と表示され分析時に取り回しがしやすいよう工夫をしてみましょう。

実践

SQLの処理順としては以下のようになります。

  1. 期間の日付を格納した配列を作成
  2. 欠落日付を含んだテーブルを作成
  3. 前述の2工程を結合。この際に値を変換

実際のコードはこちらです。

WITH
duration AS (
  SELECT *
  FROM UNNEST(GENERATE_DATE_ARRAY( DATE('2022-09-01'), DATE('2022-09-07') )) AS date
),
tbl AS (
  SELECT date, url, count
  FROM `project01.sample.table01` 
)

SELECT 
  date,
  COALESCE(url, "https://www.example.com/page01/") AS url,
  COALESCE(count, 0) AS count,

FROM duration
LEFT JOIN tbl USING (date)

解説

今回の肝となる部分です。

duration AS (
  SELECT *
  FROM UNNEST(GENERATE_DATE_ARRAY( DATE('2022-09-01'), DATE('2022-09-07') )) AS date
),

2022/09/01〜2022/09/07の期間の配列をGENERATE_DATE_ARRAYというBigQueryの関数で生成し、UNNESTにより展開しています。
分析の期間を変更することで任意の期間の配列を生成できます。

GENERATE_DATE_ARRAYは非常に便利な関数ですので覚えておきましょう
https://cloud.google.com/bigquery/docs/reference/standard-sql/functions-and-operators?hl=ja#generate_date_array

そうすることで、下記のような1日ずつの日付レコードが生成されます。

date
12022-09-01
22022-09-02
32022-09-03
42022-09-04
52022-09-05
62022-09-06
72022-09-07

次に先ほどの準備で用意したテーブルをそのまま抽出します。

tbl AS (
  SELECT date, url, count
  FROM `project01.sample.table01` 
)

最後に工程1、2で抽出したテーブルを結合します。

SELECT 
  date,
  COALESCE(url, "https://www.example.com/page01/") AS url,
  COALESCE(count, 0) AS count,

FROM duration
LEFT JOIN tbl USING (date)

この時、コード上ではCOALESCEを使って値の置換を行っていますが、使わないとどうなるかの比較を以下に添付します。

置換なし

dateurlcount
12022-09-01https://www.example.com/page01/2
22022-09-02https://www.example.com/page01/3
32022-09-03https://www.example.com/page01/4
42022-09-04nullnull
52022-09-05https://www.example.com/page01/6
62022-09-06nullnull
72022-09-07https://www.example.com/page01/8

置換あり

dateurlcount
12022-09-01https://www.example.com/page01/2
22022-09-02https://www.example.com/page01/3
32022-09-03https://www.example.com/page01/4
42022-09-04https://www.example.com/page01/0
52022-09-05https://www.example.com/page01/6
62022-09-06https://www.example.com/page01/0
72022-09-07https://www.example.com/page01/8

分析時において取り回しがしやすくなっていることが想像できると思います。

GA4でページビュー数やイベント数を抽出する際に有用です。
今回は欠落日付を補完するテクニックのご紹介でした。

関連記事

特集記事

コメント

  1. Major thanks for the article. Much obliged.

  2. I really liked your blog post.Really looking forward to read more. Fantastic.

  3. Thanks again for the blog article.Thanks Again. Really Great.

  4. Thanks again for the blog post.Thanks Again. Really Cool.

  5. Enjoyed every bit of your article post. Really Cool.

  6. Thank you ever so for you article. Really Great.

  7. It’s going to be end of mine day, however before end I am reading this wonderful piece of writing to increase my experience.

  8. Really enjoyed this article.Much thanks again.

  9. I appreciate you sharing this blog post. Will read on…

  10. I am so grateful for your blog article.Really looking forward to read more. Awesome.

  11. Fine way of telling, and fastidious piece of writingto obtain information regarding my presentation topic, whichi am going to convey in school.

  12. Thank you for your blog article. Fantastic.

  13. I do believe all the concepts you have offered to your post.They’re really convincing and can definitely work.Nonetheless, the posts are very quick for novices.Could you please extend them a little from subsequent time?Thanks for the post.

  14. This is one awesome article post.Really looking forward to read more. Really Great.

  15. I cannot thank you enough for the article post. Fantastic.

  16. I really like and appreciate your article.Really thank you! Great.

  17. Film, “Fly Girls.””Commending Females AirforcePilots,” U.S. Congressional Record.

  18. I truly appreciate this article post. Really looking forward to read more. Much obliged. Flora Ive Moynahan

  19. I blog frequently and I truly thank you for your content. Your article has really peaked my interest. I am going to bookmark your blog and keep checking for new details about once a week. I opted in for your Feed as well.

  20. logan park apartments rentberry scam ico 30m$ raised apartments in california

  21. Thanks for sharing, this is a fantastic blog post.Really looking forward to read more. Great.

  22. Hello there! Would you mind if I share your blog with my zynga group? There’s a lot of folks that I think would really enjoy your content. Please let me know. Thank you

  23. Awesome! Its actually awesome paragraph,I have got much clear idea about from this post.

  24. Looking forward to reading more. Great article post.Much thanks again. Really Cool.

  25. Fantastic article post.Really looking forward to read more.

  26. Different types of stainless steel are created depending on the amount of nickel and also chromium in the alloy. Chrome, short for chromium,

  27. Great read. This is an excellent, an eye-opener for sure! I really like your article. I enjoyed reading what you had to say.

  28. These are actually impressive ideas in on the topic of blogging. You have touched some pleasant points here. Any way keep up wrinting.

  29. You can certainly see your enthusiasm within the work you write. The arena hopes for even more passionate writers such as you who aren’t afraid to mention how they believe. At all times go after your heart.

  30. college essay how to write an essay my homework type my essay

  31. It is in reality a nice and useful piece of information. I’m glad that youshared this helpful info with us. Please stay us up to date like this.Thank you for sharing.

  32. My relatives always say that I am killing my time here at net, except I know I am getting experience all the time by reading suchpleasant posts.

  33. A round of applause for your article.Really looking forward to read more.

  34. This is one awesome article post.Much thanks again. Much obliged.

  35. Im grateful for the blog.Really thank you! Much obliged.

  36. They might be either affordable or expensive (but solar sections are certainly worth considering) based on your requirements

  37. You could also hold charity football games to raise money for local organizations as well.

  38. Thanks-a-mundo for the article. Fantastic.

  39. I wanted to thank you for this fantastic read!! I definitely enjoyed every bit of it. I have got you saved as a favorite to look at new stuff you postÖ

  40. Thank you for the good writeup. It in fact was a amusement account it.Look advanced to far added agreeable from you! By theway, how can we communicate?

  41. order generic vardenafil online – vardenafil prix generic vardenafil review

  42. ivermectin pills human ivermectin cream 1 – ivermectin 250ml

  43. Hello, all is going nicely here and ofcourse every one is sharing facts, that’sin fact fine, keep up writing.

  44. best male enhancement pills legitimate online pharmacies india — ed medicine online

  45. chloroquine phosphate generic name how does plaquenil work

  46. ivermectin humans ivermectin for humans for sale

  47. Hey, thanks for the article.Really looking forward to read more. Want more.

  48. Major thankies for the article. Much obliged.

  49. I need to to thank you for this good read!! I absolutely loved every bit of it. I have you book marked to look at new stuff you postÖ

  50. rs also want to make revenue.It really is no wonder that lots of bettors domany factors to get and makeDiscord Free Accounts 2021 Discord Account And Passwordfree discord accounts

  51. Fantastic blog article.Much thanks again. Great.

  52. Im obliged for the blog article.Much thanks again.

  53. Wow that was unusual. I just wrote an incredibly long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Anyhow, just wanted to say superb blog!

  54. Thanks a lot for the post.Much thanks again. Awesome.

  55. Amazing! Its actually remarkable piece of writing, I have got much clear idea on the topic of from this paragraph.

  56. ivermectin topical rosacea fenbendazole vs ivermectin

  57. I’ll immediately clutch your rss feed as I can’t find your email subscription hyperlink ore-newsletter service. Do you have any? Please allow me know so that I could subscribe.Thanks.

  58. I’m now not positive the place you’re getting your information, but great topic. I needs to spend some time finding out more or working out more. Thanks for excellent information I was on the lookout for this information for my mission.

  59. Your comments may take several hours to import.

  60. Do military services has started on YouTube, as for were trying to find your own video.

  61. Thank you ever so for you article post.Really thank you! Will read on…

  62. Great, thanks for sharing this blog article.Really thank you! Cool.

  63. Appreciate you sharing, great post.Really looking forward to read more.

  64. A round of applause for your article post.Really looking forward to read more. Fantastic.

  65. Very neat post.Much thanks again. Fantastic.

  66. Hello, yup this article is truly nice and I have learned lot of things from it about blogging. thanks.

  67. Incredible story there. What happened after? Good luck!

  68. Very neat blog article.Really looking forward to read more. Will read on…

  69. Very nice post. I just stumbled upon your blog and wished to say that I’ve really enjoyedsurfing around your blog posts. In any case I will be subscribing to your rss feed and I hope you writeagain soon!

  70. ivermectin over the counter – stromectol order ivermectin human

  71. I need to to thank you for this very good read!! I certainly loved every little bit of it.I have you saved as a favorite to check out new stuff you post…

  72. I do believe you will find there’s problem with your site making use of Internet explorer browser.

  73. linetogel

  74. sneakers

  75. The information shared is of top quality which has to get appreciated at all levels. Well done…

  76. Thanks for your article. I have continually observed that a majority of people are needing to lose weight as they wish to show up slim and attractive. Nonetheless, they do not often realize that there are additional benefits just for losing weight also. Doctors say that over weight people suffer from a variety of ailments that can be directly attributed to their particular excess weight. Thankfully that people who’re overweight and suffering from various diseases can help to eliminate the severity of their own illnesses by way of losing weight. It is easy to see a steady but notable improvement in health if even a minor amount of weight loss is accomplished.

ランキング(週間)

  1. 1

    GA4 「ページ ロケーション」と「ページ パス と スクリーン クラス」何が違う?ドメインの有無以外にもパラメータの扱いに違いあり

  2. 2

    GA4の探索で「ページ別訪問数」を見ることは可能か?

  3. 3

    GA4 拡張計測機能の新顔「フォームの操作」 form_start, form_submit

  4. 4

    GA4のレポート画面の数値データは、イベント発生時から何時間後に確定するのか?

  5. 5

    【祝・GPT-4 が ChatGPT plus に登場記念!】アクセス解析コンサルタントが、GPT-4にCSVデータを渡して、データ解析結果のコメントを書いてもらう方法

  6. 6

    GA4の指標「総ユーザー数」VS「アクティブ ユーザー数」

  7. 7

    GA4 クロスドメイン設定では、リンク先に「_gl」パラメータが付く。勝手なパラメータを付けるとエラーになるページは要注意

  8. 8

    GA4 イベントタグの「詳細設定」に「e コマースデータを送信」が実装されました。

  9. 9

    GA4で異なるドメイン(サブドメイン)の同一ページパスをドメイン付きで表示する

  10. 10

    GA4では、utm_term, utm_content はどうなったのか?

最近の記事

  1. 生成AIを調整してABテストを実装したら想像以上に早かった

  2. 無料で使えるABテストツールのバージョンアップを行いました

  3. 無料ABテストツール(and,B)のマニュアルを作成しました

カテゴリー

 
TOP