Skip to content

Instantly share code, notes, and snippets.

@yyoshiki41
Last active January 23, 2017 00:50
Show Gist options
  • Save yyoshiki41/8fb7a0bc4957cf61397ef5ecc23a5c43 to your computer and use it in GitHub Desktop.
Save yyoshiki41/8fb7a0bc4957cf61397ef5ecc23a5c43 to your computer and use it in GitHub Desktop.
過去2週間分の登録者数から、着地予想を出す。(線形近似)
SELECT
SUM(
CASE WHEN registered_at >= DATE_TRUNC('month', now()) THEN 1 ELSE 0 END
) AS COUNT,
SUM(
CASE WHEN registered_at >= DATE_TRUNC('month', now()) THEN 1 ELSE 0 END
) + SUM(
CASE WHEN registered_at BETWEEN DATE_TRUNC('day', now() + '-14 days') AND DATE_TRUNC('day', now()) THEN 1 ELSE 0 END
) / 14 * (
date_part('day', DATE_TRUNC('month', now() + '1 months') + '-1 days') - date_part('day', DATE_TRUNC('day', now()))
) AS forecast
FROM user
WHERE registered_at >= DATE_TRUNC('month', now() + '-14 days')
@yyoshiki41
Copy link
Author

forecast = 現在の登録者数 + 過去14日間の平均デイリー登録者数 * 今月の残り日数

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment