Advertisement
festinko

Untitled

Dec 23rd, 2022
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.57 KB | None | 0 0
  1. WITH
  2.     preparedData as (
  3.         SELECT
  4.             p.userID,
  5.       p.productID,
  6.       pr.productValue,
  7.             paymentAmount,
  8.             paymentPeriod,
  9.             DATE(paymentDateTime) as paymentDateTime,
  10.             DATE_TRUNC(DATETIME(paymentDateTime), MONTH) as prepaidPeriodStart,
  11.       DATE_TRUNC(DATE_ADD(DATETIME(paymentDateTime), INTERVAL paymentPeriod MONTH), MONTH) as prepaidPeriodEnd,
  12.       DATE_TRUNC(DATE_ADD(DATETIME(paymentDateTime), INTERVAL paymentPeriod MONTH), DAY) as prepaidPeriodEndDay,
  13.             DATE_ADD(DATETIME(paymentDateTime), INTERVAL paymentPeriod MONTH) as prepaidPeriodEndDateTime
  14.         FROM
  15.             `uptimerobot-001.ur.payments` p
  16.  
  17.     LEFT JOIN
  18.     `uptimerobot-001.ur.products` pr
  19.      
  20.       ON p.productID = pr.productID
  21.  
  22.         LEFT JOIN   (
  23.             SELECT
  24.                 userID,
  25.                 MAX(paymentDateTime) lastPaymentDateTime
  26.             FROM
  27.                 `uptimerobot-001.ur.payments`
  28.            
  29.             WHERE
  30.                 paymentStatus = 1
  31.                 AND paymentPeriod IN (1,12)
  32.                 AND userID IS NOT NULL
  33.             GROUP BY
  34.                 userID
  35.             ) lp
  36.             ON p.userID =lp.userID AND p.paymentDateTime = lp.lastPaymentDateTime
  37.  
  38.         WHERE
  39.             paymentStatus = 1
  40.             AND paymentPeriod IN (1,12)
  41.             AND p.userID IS NOT NULL
  42.             AND lp.lastPaymentDateTime IS NOT NULL
  43.     ),
  44.     calendar as (
  45.         SELECT
  46.         DATE_TRUNC(DATETIME(paymentDateTime), DAY) as reportingYearMonthDay
  47.         FROM `uptimerobot-001.ur.payments`
  48.         GROUP BY
  49.             reportingYearMonthDay
  50.     ),
  51.  
  52.   churnedCustomersDaily as (
  53.     SELECT
  54.         reportingYearMonthDay,
  55.         COUNT(DISTINCT userID) churnedCustomersCount,
  56.         (CASE
  57.             WHEN DATE_ADD(reportingYearMonthDay, INTERVAL 17 DAY) >= CURRENT_DATE THEN 1
  58.             WHEN DATE_ADD(reportingYearMonthDay, INTERVAL 17 DAY) < CURRENT_DATE THEN 0
  59.         ELSE 99999999999999
  60.         END) isGracePeriod
  61.  
  62.   FROM
  63.     preparedData pd
  64.    
  65.     LEFT JOIN calendar cal
  66.         ON pd.prepaidPeriodEndDateTime > reportingYearMonthDay AND pd.prepaidPeriodEndDay < DATE_ADD(cal.reportingYearMonthDay, INTERVAL 1 DAY)
  67.    
  68.     WHERE
  69.     reportingYearMonthDay IS NOT NULL
  70.  
  71.     GROUP BY
  72.         reportingYearMonthDay
  73.    
  74.     ORDER BY
  75.         reportingYearMonthDay DESC),
  76.  
  77.   currentCustomersDaily as (
  78.    
  79.     SELECT
  80.         reportingYearMonthDay,
  81.         COUNT(DISTINCT userID) currentCustomersCount,
  82.  
  83.   FROM
  84.     `uptimerobot-001.ur.payments` p
  85.    
  86.     LEFT JOIN calendar cal
  87.         ON DATE(p.paymentDateTime) < reportingYearMonthDay AND DATE_TRUNC(DATE_ADD(DATETIME(p.paymentDateTime), INTERVAL paymentPeriod MONTH), DAY) > reportingYearMonthDay
  88.    
  89.     WHERE
  90.     reportingYearMonthDay IS NOT NULL
  91.  
  92.     GROUP BY
  93.         reportingYearMonthDay
  94.    
  95.     ORDER BY
  96.         reportingYearMonthDay DESC)
  97.  
  98.     SELECT
  99.         *
  100.    
  101.     FROM
  102.         currentCustomersDaily
  103.    
  104.     ORDER BY
  105.     reportingYearMonthDay DESC
  106.  
  107.  
  108.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement