Advertisement
festinko

Untitled

Sep 12th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.10 KB | None | 0 0
  1. WITH
  2.  
  3. /*List of all successful subscription payments - Alltime*/
  4.     SuccessSubsPayment AS(
  5.          SELECT
  6.             userID,
  7.             paymentDateTime,
  8.             paymentPeriod,
  9.             productID
  10.  
  11.          FROM `uptimerobot-001.ur.payments`
  12.          WHERE paymentType = 2 AND paymentStatus = 1
  13.      ),
  14.  
  15. /*Last successful subscription payments by user*/
  16.      LastSuccessSubsPayment AS(
  17.          SELECT
  18.             p1.userID,
  19.             p1.paymentDateTime as lastPaymentDateTime,
  20.             p1.paymentPeriod as lastPaymentPeriod,
  21.             p1.productID as lastPaymentProductID
  22.  
  23.         FROM SuccessSubsPayment p1
  24.         LEFT JOIN SuccessSubsPayment p2
  25.         ON (p1.userID = p2.userID AND p1.paymentDateTime < p2.paymentDateTime)
  26.         WHERE
  27.             p2.userID IS NULL
  28.  
  29.      ),
  30.  
  31. /*added userType - Current / Churned Customer*/
  32.     LastSubsPaymentUsersType AS (
  33.             SELECT
  34.                 userID,
  35.                 lastPaymentDateTime,
  36.                 lastPaymentPeriod,
  37.                 (CASE
  38.                     WHEN (DATE_ADD(CAST(lastPaymentDateTime AS DATETIME), INTERVAL lastPaymentPeriod MONTH) >= CURRENT_DATE()) THEN 'CurrentCustomer'
  39.                     ELSE 'ChurnedCustomer'
  40.                 END) AS customerType,
  41.                 lastPaymentProductID
  42.             FROM LastSuccessSubsPayment
  43.     ),
  44.  
  45. /*added productName*/
  46.     LastSubsPaymentUsersTypeProduct AS (
  47.             SELECT
  48.                 lsput.userID,
  49.                 lsput.lastPaymentDateTime,
  50.                 lsput.lastPaymentPeriod,
  51.                 lsput.customerType,
  52.                 lsput.lastPaymentProductID,
  53.                 pr.productName
  54.  
  55.             FROM
  56.                 LastSubsPaymentUsersType as lsput
  57.             LEFT JOIN `uptimerobot-001.ur.products` as pr
  58.                 ON pr.productID = lsput.lastPaymentProductID
  59.     ),
  60.  
  61. /*Deduplication of multiple last successful payments from the same user - some edge cases*/
  62.  LastSuccessSubsPaymentUserTypeProduct AS (
  63.     SELECT
  64.         userID,
  65.         MAX(lastPaymentDateTime) as lastPaymentDateTime,
  66.         MAX(lastPaymentPeriod) as lastPaymentPeriod,
  67.         MAX(customerType) as customerType,
  68.         MAX(lastPaymentProductID) as lastPaymentProductID,
  69.         MAX(productName) as lastPaymentProductName
  70.  
  71.     FROM LastSubsPaymentUsersTypeProduct
  72.  
  73.     GROUP BY userID
  74.     ORDER BY userID DESC
  75.     ),
  76.    
  77. /*List of All Active Telegram Contacts*/
  78.     TelegramAlertContacts AS (
  79.  
  80.         SELECT
  81.             userID,
  82.             alertContactType,
  83.             alertContactValue,
  84.             alertContactStatus
  85.         FROM `uptimerobot-001.ur.alertcontacts`
  86.         WHERE alertContactType = 18 AND alertContactStatus = 2
  87.         ),
  88.  
  89. /*Number of Active Telegram Contacts per user*/
  90.     CountOfUniqueTelegramAlertContactsPerUser AS(
  91.         SELECT
  92.             userID,
  93.             count(DISTINCT alertContactValue) uniqueEmailAlertContacts
  94.     FROM TelegramAlertContacts
  95.     GROUP BY userID
  96.     ORDER BY uniqueEmailAlertContacts DESC
  97.  
  98.     ),
  99.  
  100. /*List of all users with the count of active unique Telegram by user type - FREE / Current / Churned Customer & product name*/
  101. TelegramContacts AS(
  102.     SELECT
  103.         CountOfUniqueTelegramAlertContactsPerUser.userID,
  104.         uniqueEmailAlertContacts,
  105.         (CASE
  106.             WHEN customerType IS NULL THEN 'FREE'
  107.             ELSE customerType
  108.         END) as customerType,
  109.         (CASE
  110.             WHEN lastPaymentProductName IS NULL THEN 'FREE'
  111.             ELSE lastPaymentProductName
  112.         END) as lastPaymentProductName
  113.        
  114.  
  115.     FROM CountOfUniqueTelegramAlertContactsPerUser
  116.     LEFT JOIN LastSuccessSubsPaymentUserTypeProduct
  117.         on CountOfUniqueTelegramAlertContactsPerUser.userID = LastSuccessSubsPaymentUserTypeProduct.userID
  118.     )
  119. /*Number of Users with active Telegram by customer type and lastPaymentProduct*/
  120. SELECT
  121.     customerType,
  122.     lastPaymentProductName,
  123.     count(userID) as userCount
  124. FROM TelegramContacts
  125. GROUP BY
  126.     customerType,
  127.     lastPaymentProductName
  128. ORDER BY
  129.     customerType,
  130.     userCount DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement