festinko

Untitled

Oct 25th, 2022
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 6.63 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
  13.           paymentType = 2
  14.           AND paymentStatus = 1
  15.      ),
  16.  
  17. /*Last successful subscription payments by user*/
  18.      LastSuccessSubsPayment AS(
  19.          SELECT
  20.             p1.userID,
  21.             p1.paymentDateTime as lastPaymentDateTime,
  22.             p1.paymentPeriod as lastPaymentPeriod,
  23.             p1.productID as lastPaymentProductID
  24.  
  25.         FROM SuccessSubsPayment p1
  26.         LEFT JOIN SuccessSubsPayment p2
  27.         ON (p1.userID = p2.userID AND p1.paymentDateTime < p2.paymentDateTime)
  28.         WHERE
  29.             p2.userID IS NULL
  30.  
  31.      ),
  32.  
  33. /*added userType - Current / Churned Customer*/
  34.     LastSubsPaymentUsersType AS (
  35.             SELECT
  36.                 userID,
  37.                 lastPaymentDateTime,
  38.                 lastPaymentPeriod,
  39.                 (CASE
  40.                     WHEN (DATE_ADD(DATETIME(lastPaymentDateTime), INTERVAL lastPaymentPeriod MONTH) >= CURRENT_DATE()) THEN 'CurrentCustomer'
  41.                     ELSE 'ChurnedCustomer'
  42.                 END) AS customerType,
  43.                 lastPaymentProductID
  44.             FROM LastSuccessSubsPayment
  45.     ),
  46.  
  47. /*added productName & ProductValue*/
  48.     LastSubsPaymentUsersTypeProduct AS (
  49.             SELECT
  50.                 lsput.userID,
  51.                 lsput.lastPaymentDateTime,
  52.                 lsput.lastPaymentPeriod,
  53.                 lsput.customerType,
  54.                 lsput.lastPaymentProductID,
  55.                 pr.productName,
  56.                 pr.productValue
  57.  
  58.             FROM
  59.                 LastSubsPaymentUsersType as lsput
  60.             LEFT JOIN `uptimerobot-001.ur.products` as pr
  61.                 ON pr.productID = lsput.lastPaymentProductID
  62.     ),
  63.  
  64. /*Deduplication of multiple last successful payments from the same user - some edge cases*/
  65.  LastSuccessSubsPaymentUserTypeProduct AS (
  66.     SELECT
  67.         userID,
  68.         MAX(lastPaymentDateTime) as lastPaymentDateTime,
  69.         MAX(lastPaymentPeriod) as lastPaymentPeriod,
  70.         MAX(customerType) as customerType,
  71.         MAX(lastPaymentProductID) as lastPaymentProductID,
  72.         MAX(productName) as lastPaymentProductName,
  73.         MAX(productValue) as lastPaymentProductValue
  74.  
  75.     FROM LastSubsPaymentUsersTypeProduct
  76.  
  77.     GROUP BY userID
  78.     ORDER BY userID DESC
  79.     ),
  80.    
  81. /*List of All Active Not Default E-mail Alert Contacts*/
  82.     NotDefaultEmailAlertContacts AS (
  83.  
  84.         SELECT
  85.             userID,
  86.             alertContactType,
  87.             alertContactValue,
  88.             alertContactStatus,
  89.             rand() as random
  90.         FROM `uptimerobot-001.ur.alertcontacts`
  91.         WHERE
  92.           alertContactType = 2
  93.           AND alertContactStatus = 2
  94.         ),
  95.  
  96. /*Number of Active Unique E-mail Alert Contacts per user*/
  97.     CountOfUniqueEmailAlertContactsPerUser AS(
  98.         SELECT
  99.             userID,
  100.             count(DISTINCT alertContactValue) uniqueEmailAlertContacts
  101.         FROM NotDefaultEmailAlertContacts
  102.         GROUP BY userID
  103.         ORDER BY uniqueEmailAlertContacts DESC
  104.  
  105.     ),
  106.  
  107. /*List of all users with the count of active unique not default Email Alert Contacts by user type - FREE / Current / Churned Customer & productName & productValue*/
  108. EmailAlertContacts AS(
  109.     SELECT
  110.         CountOfUniqueEmailAlertContactsPerUser.userID,
  111.         uniqueEmailAlertContacts,
  112.         (CASE
  113.             WHEN customerType IS NULL THEN 'FREE'
  114.             ELSE customerType
  115.         END) as customerType,
  116.         (CASE
  117.             WHEN lastPaymentProductName IS NULL THEN 'FREE'
  118.             ELSE lastPaymentProductName
  119.         END) as lastPaymentProductName,
  120.         (CASE
  121.             WHEN lastPaymentProductValue IS NULL THEN 0
  122.             ELSE lastPaymentProductValue
  123.         END) as lastPaymentProductValue
  124.        
  125.  
  126.     FROM CountOfUniqueEmailAlertContactsPerUser
  127.     LEFT JOIN LastSuccessSubsPaymentUserTypeProduct
  128.         on CountOfUniqueEmailAlertContactsPerUser.userID = LastSuccessSubsPaymentUserTypeProduct.userID
  129.     )
  130. /*Number of Users with active Not Default Email AlertContact by customer type and lastPaymentProduct*/
  131. /*
  132. SELECT
  133.     customerType,
  134.     lastPaymentProductName,
  135.     count(userID) as userCount
  136. FROM EmailAlertContacts
  137. GROUP BY
  138.     customerType,
  139.     lastPaymentProductName,
  140.     lastPaymentProductValue
  141. ORDER BY
  142.     customerType,
  143.     lastPaymentProductValue*/
  144.  
  145.     SELECT
  146.         NotDefaultEmailAlertContacts.userID,
  147.         MD5(CAST(NotDefaultEmailAlertContacts.userID as string)) hashedUserID,
  148.         DENSE_RANK() OVER (PARTITION BY customerType ORDER BY MD5(CAST(NotDefaultEmailAlertContacts.userID as string))) as rank,
  149.         (CASE
  150.             WHEN
  151.                 userEmail IS NULL THEN '_onlyDefaultEmailAlertContact'
  152.                 ELSE userEmail
  153.             END) userEmailDefault,
  154.         alertContactType,
  155.         alertContactValue,
  156.         (CASE
  157.             WHEN alertContactValue = userEmail THEN '1'
  158.             WHEN alertContactValue <> userEmail THEN '0'
  159.         ELSE 'WTF'
  160.         END
  161.         ) isDefaultAlertContact,
  162.         alertContactStatus,
  163.         uniqueEmailAlertContacts,
  164.         (CASE
  165.             WHEN uniqueEmailAlertContacts > 6 THEN '>6uniqueEmailAlertContacts'
  166.             WHEN uniqueEmailAlertContacts <= 6 THEN '<=6uniqueEmailAlertContacts'
  167.             ELSE 'WTF'
  168.         END) emailAlertContactsBucket,
  169.         (CASE
  170.             WHEN customerType IS NULL THEN 'FREE'
  171.             ELSE customerType
  172.         END) as customerType,
  173.         (CASE
  174.             WHEN lastPaymentProductName IS NULL THEN 'FREE'
  175.             ELSE lastPaymentProductName
  176.         END) as lastPaymentProductName,
  177.         (CASE
  178.             WHEN lastPaymentProductValue IS NULL THEN 0
  179.             ELSE lastPaymentProductValue
  180.         END) as lastPaymentProductValue
  181.        
  182.  
  183.     FROM
  184.         NotDefaultEmailAlertContacts
  185.  
  186.     LEFT JOIN
  187.         CountOfUniqueEmailAlertContactsPerUser
  188.     ON NotDefaultEmailAlertContacts.userID = CountOfUniqueEmailAlertContactsPerUser.userID
  189.     LEFT JOIN
  190.         `uptimerobot-001.ur.users` u
  191.     ON NotDefaultEmailAlertContacts.userID = u.userID
  192.     LEFT JOIN LastSuccessSubsPaymentUserTypeProduct
  193.     ON NotDefaultEmailAlertContacts.userID = LastSuccessSubsPaymentUserTypeProduct.userID
  194.    
  195.     WHERE NotDefaultEmailAlertContacts.userID IS NOT NULL
  196.  
  197.     ORDER BY
  198.         customerType DESC,
  199.         hashedUserID,
  200.         emailAlertContactsBucket,
  201.         userID DESC
  202.        
  203.  
Add Comment
Please, Sign In to add comment