Advertisement
festinko

Untitled

Oct 24th, 2022
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 5.86 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.         FROM `uptimerobot-001.ur.alertcontacts`
  90.         WHERE
  91.           alertContactType = 2
  92.           AND alertContactStatus = 2
  93.         ),
  94.  
  95. /*Number of Active Unique E-mail Alert Contacts per user*/
  96.     CountOfUniqueEmailAlertContactsPerUser AS(
  97.         SELECT
  98.             userID,
  99.             count(DISTINCT alertContactValue) uniqueEmailAlertContacts
  100.         FROM NotDefaultEmailAlertContacts
  101.         GROUP BY userID
  102.         ORDER BY uniqueEmailAlertContacts DESC
  103.  
  104.     ),
  105.  
  106. /*List of all users with the count of active unique not default Email Alert Contacts by user type - FREE / Current / Churned Customer & productName & productValue*/
  107. EmailAlertContacts AS(
  108.     SELECT
  109.         CountOfUniqueEmailAlertContactsPerUser.userID,
  110.         uniqueEmailAlertContacts,
  111.         (CASE
  112.             WHEN customerType IS NULL THEN 'FREE'
  113.             ELSE customerType
  114.         END) as customerType,
  115.         (CASE
  116.             WHEN lastPaymentProductName IS NULL THEN 'FREE'
  117.             ELSE lastPaymentProductName
  118.         END) as lastPaymentProductName,
  119.         (CASE
  120.             WHEN lastPaymentProductValue IS NULL THEN 0
  121.             ELSE lastPaymentProductValue
  122.         END) as lastPaymentProductValue
  123.        
  124.  
  125.     FROM CountOfUniqueEmailAlertContactsPerUser
  126.     LEFT JOIN LastSuccessSubsPaymentUserTypeProduct
  127.         on CountOfUniqueEmailAlertContactsPerUser.userID = LastSuccessSubsPaymentUserTypeProduct.userID
  128.     )
  129. /*Number of Users with active Not Default Email AlertContact by customer type and lastPaymentProduct*/
  130. /*
  131. SELECT
  132.     customerType,
  133.     lastPaymentProductName,
  134.     count(userID) as userCount
  135. FROM EmailAlertContacts
  136. GROUP BY
  137.     customerType,
  138.     lastPaymentProductName,
  139.     lastPaymentProductValue
  140. ORDER BY
  141.     customerType,
  142.     lastPaymentProductValue*/
  143.  
  144.     SELECT
  145.         NotDefaultEmailAlertContacts.userID,
  146.         (CASE
  147.             WHEN
  148.                 userEmail IS NULL THEN '_onlyDefaultEmailAlertContact'
  149.                 ELSE userEmail
  150.             END) userEmailDefault,
  151.         alertContactType,
  152.         alertContactValue,
  153.         alertContactStatus,
  154.         uniqueEmailAlertContacts,
  155.         (CASE
  156.             WHEN customerType IS NULL THEN 'FREE'
  157.             ELSE customerType
  158.         END) as customerType,
  159.         (CASE
  160.             WHEN lastPaymentProductName IS NULL THEN 'FREE'
  161.             ELSE lastPaymentProductName
  162.         END) as lastPaymentProductName,
  163.         (CASE
  164.             WHEN lastPaymentProductValue IS NULL THEN 0
  165.             ELSE lastPaymentProductValue
  166.         END) as lastPaymentProductValue
  167.        
  168.  
  169.     FROM
  170.         NotDefaultEmailAlertContacts
  171.  
  172.     LEFT JOIN
  173.         CountOfUniqueEmailAlertContactsPerUser
  174.     ON NotDefaultEmailAlertContacts.userID = CountOfUniqueEmailAlertContactsPerUser.userID
  175.     LEFT JOIN
  176.         `uptimerobot-001.ur.users` u
  177.     ON NotDefaultEmailAlertContacts.userID = u.userID
  178.     LEFT JOIN LastSuccessSubsPaymentUserTypeProduct
  179.     ON NotDefaultEmailAlertContacts.userID = LastSuccessSubsPaymentUserTypeProduct.userID
  180.  
  181.     ORDER BY
  182.         userID DESC,
  183.         uniqueEmailAlertContacts DESC
  184.  
  185.    
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement