<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<oembed>
  <author_name>chito_ng</author_name>
  <author_url>https://blog.hatena.ne.jp/chito_ng/</author_url>
  <blog_title>まずは蝋の翼から。</blog_title>
  <blog_url>https://knknkn.hatenablog.com/</blog_url>
  <categories>
    <anon>SQL</anon>
  </categories>
  <description>ユーザー毎の最長連続課金日数を求める。 Redshiftを想定。 データイメージ 「課金」列はその日課金した場合は1、課金してなかった場合は0となる。 『連続して○○した日数』をどうやってSQLで求めるか？がポイントとなります。 SQLなので、集約関数やWINDOW関数などを使えば簡単に連続した日数を計算できると思いきや、 SUM関数を使おうにも、下図のように、「同じuser_id」かつ「連続している日」を集計領域にする必要があり、案外頭を使った。 ポイント 「同じuser_idで課金日が連続している」部分を集計領域にした集計関数を使う。 そのために、「同じuser_id」かつ「課金している日…</description>
  <height>190</height>
  <html>&lt;iframe src=&quot;https://hatenablog-parts.com/embed?url=https%3A%2F%2Fknknkn.hatenablog.com%2Fentry%2F2018%2F05%2F05%2F164824&quot; title=&quot;SQLで連続課金日数を計算し、その最大値を求める - まずは蝋の翼から。&quot; class=&quot;embed-card embed-blogcard&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; style=&quot;display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;&quot;&gt;&lt;/iframe&gt;</html>
  <image_url>https://cdn-ak.f.st-hatena.com/images/fotolife/c/chito_ng/20180505/20180505151641.png</image_url>
  <provider_name>Hatena Blog</provider_name>
  <provider_url>https://hatena.blog</provider_url>
  <published>2018-05-05 16:48:24</published>
  <title>SQLで連続課金日数を計算し、その最大値を求める</title>
  <type>rich</type>
  <url>https://knknkn.hatenablog.com/entry/2018/05/05/164824</url>
  <version>1.0</version>
  <width>100%</width>
</oembed>
