Advertisement
festinko

Untitled

Sep 26th, 2022
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.89 KB | None | 0 0
  1. WITH
  2.     preparedData as (
  3.         SELECT
  4.             userID,
  5.             paymentAmount,
  6.             paymentPeriod,
  7.             paymentDateTime,
  8.             DATE_TRUNC(DATETIME(paymentDateTime), MONTH) as prepaidPeriodStart,
  9.       DATE_TRUNC(DATE_ADD(DATETIME(paymentDateTime), INTERVAL paymentPeriod MONTH), MONTH) as prepaidPeriodEnd
  10.         FROM
  11.             `uptimerobot-001.ur.payments`
  12.         WHERE
  13.             paymentStatus = 1
  14.             AND paymentPeriod BETWEEN 1 AND 12
  15.             AND userID IS NOT NULL
  16.     ),
  17.     calendar as (
  18.         select
  19.             DATE_TRUNC(DATETIME(paymentDateTime), MONTH) as reportingYearMonth
  20.         FROM `uptimerobot-001.ur.payments`
  21.         GROUP BY reportingYearMonth
  22.     )
  23.    
  24. SELECT
  25.  cal.reportingYearMonth,
  26.  pd.paymentPeriod,
  27.  count(DISTINCT userID) activeUsers
  28. FROM calendar cal
  29. JOIN preparedData pd ON cal.reportingYearMonth BETWEEN pd.prepaidPeriodStart AND prepaidPeriodEnd
  30. GROUP BY
  31.     cal.reportingYearMonth,
  32.    pd.paymentPeriod
  33. ORDER BY reportingYearMonth DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement