festinko

Untitled

Oct 25th, 2022
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 7.65 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 Team Integration Alert Contacts*/
  82.    TeamIntegrationAlertContacts AS (
  83.  
  84.         SELECT
  85.             userID,
  86.             alertContactType,
  87.             alertContactValue,
  88.             alertContactStatus
  89.         FROM `uptimerobot-001.ur.alertcontacts`
  90.         WHERE alertContactType IN (5,7,11,15,17,18,20) AND alertContactStatus = 2
  91.         ),
  92.    
  93.   /*List users with number of unique team integrations alertContacts*/
  94.     countOfTeamIntegrationAlertContactsByUser AS (
  95.       SELECT
  96.         userID,
  97.         COUNT(DISTINCT alertContactValue) as uniqueTeamIntegrationsCount
  98.       FROM
  99.         TeamIntegrationAlertContacts
  100.      
  101.       GROUP BY
  102.         userID
  103.     ),
  104.    
  105. /*List of All Active Not Default E-mail Alert Contacts*/
  106.     NotDefaultEmailAlertContacts AS (
  107.  
  108.         SELECT
  109.             userID,
  110.             alertContactType,
  111.             alertContactValue,
  112.             alertContactStatus,
  113.             rand() as random
  114.         FROM `uptimerobot-001.ur.alertcontacts`
  115.         WHERE
  116.           alertContactType = 2
  117.           AND alertContactStatus = 2
  118.         ),
  119.  
  120. /*Number of Active Unique E-mail Alert Contacts per user*/
  121.     CountOfUniqueEmailAlertContactsPerUser AS(
  122.         SELECT
  123.             userID,
  124.             count(DISTINCT alertContactValue) uniqueEmailAlertContacts
  125.         FROM NotDefaultEmailAlertContacts
  126.         GROUP BY userID
  127.         ORDER BY uniqueEmailAlertContacts DESC
  128.  
  129.     ),
  130.  
  131. /*List of all users with the count of active unique not default Email Alert Contacts by user type - FREE / Current / Churned Customer & productName & productValue*/
  132. EmailAlertContacts AS(
  133.     SELECT
  134.         CountOfUniqueEmailAlertContactsPerUser.userID,
  135.         uniqueEmailAlertContacts,
  136.         (CASE
  137.             WHEN customerType IS NULL THEN 'FREE'
  138.             ELSE customerType
  139.         END) as customerType,
  140.         (CASE
  141.             WHEN lastPaymentProductName IS NULL THEN 'FREE'
  142.             ELSE lastPaymentProductName
  143.         END) as lastPaymentProductName,
  144.         (CASE
  145.             WHEN lastPaymentProductValue IS NULL THEN 0
  146.             ELSE lastPaymentProductValue
  147.         END) as lastPaymentProductValue
  148.        
  149.  
  150.     FROM CountOfUniqueEmailAlertContactsPerUser
  151.     LEFT JOIN LastSuccessSubsPaymentUserTypeProduct
  152.         on CountOfUniqueEmailAlertContactsPerUser.userID = LastSuccessSubsPaymentUserTypeProduct.userID
  153.     )
  154. /*Number of Users with active Not Default Email AlertContact by customer type and lastPaymentProduct*/
  155. /*
  156. SELECT
  157.     customerType,
  158.     lastPaymentProductName,
  159.     count(userID) as userCount
  160. FROM EmailAlertContacts
  161. GROUP BY
  162.     customerType,
  163.     lastPaymentProductName,
  164.     lastPaymentProductValue
  165. ORDER BY
  166.     customerType,
  167.     lastPaymentProductValue*/
  168.  
  169.     SELECT
  170.         NotDefaultEmailAlertContacts.userID,
  171.         MD5(CAST(NotDefaultEmailAlertContacts.userID as string)) hashedUserID,
  172.         DENSE_RANK() OVER (PARTITION BY customerType ORDER BY MD5(CAST(NotDefaultEmailAlertContacts.userID as string))) as rank,
  173.         (CASE
  174.             WHEN
  175.                 userEmail IS NULL THEN '_onlyDefaultEmailAlertContact'
  176.                 ELSE userEmail
  177.             END) userEmailDefault,
  178.         alertContactType,
  179.         alertContactValue,
  180.         (CASE
  181.             WHEN alertContactValue = userEmail THEN '1'
  182.             WHEN alertContactValue <> userEmail THEN '0'
  183.         ELSE 'WTF'
  184.         END
  185.         ) isDefaultAlertContact,
  186.         alertContactStatus,
  187.         uniqueEmailAlertContacts,
  188.         (CASE
  189.             WHEN uniqueEmailAlertContacts > 6 THEN '>6uniqueEmailAlertContacts'
  190.             WHEN uniqueEmailAlertContacts <= 6 THEN '<=6uniqueEmailAlertContacts'
  191.             ELSE 'WTF'
  192.         END) emailAlertContactsBucket,
  193.         (CASE
  194.             WHEN customerType IS NULL THEN 'FREE'
  195.             ELSE customerType
  196.         END) as customerType,
  197.         (CASE
  198.             WHEN lastPaymentProductName IS NULL THEN 'FREE'
  199.             ELSE lastPaymentProductName
  200.         END) as lastPaymentProductName,
  201.         (CASE
  202.             WHEN lastPaymentProductValue IS NULL THEN 0
  203.             ELSE lastPaymentProductValue
  204.         END) as lastPaymentProductValue,
  205.         (CASE
  206.             WHEN uniqueTeamIntegrationsCount IS NULL THEN 0
  207.             ELSE uniqueTeamIntegrationsCount
  208.         END) uniqueTeamIntegrationsCount
  209.        
  210.  
  211.     FROM
  212.         NotDefaultEmailAlertContacts
  213.  
  214.     LEFT JOIN
  215.         CountOfUniqueEmailAlertContactsPerUser
  216.     ON NotDefaultEmailAlertContacts.userID = CountOfUniqueEmailAlertContactsPerUser.userID
  217.    
  218.     LEFT JOIN
  219.         `uptimerobot-001.ur.users` u
  220.     ON NotDefaultEmailAlertContacts.userID = u.userID
  221.    
  222.     LEFT JOIN LastSuccessSubsPaymentUserTypeProduct
  223.     ON NotDefaultEmailAlertContacts.userID = LastSuccessSubsPaymentUserTypeProduct.userID
  224.  
  225.     LEFT JOIN countOfTeamIntegrationAlertContactsByUser
  226.     ON NotDefaultEmailAlertContacts.userID = countOfTeamIntegrationAlertContactsByUser.userID
  227.  
  228.    
  229.     WHERE NotDefaultEmailAlertContacts.userID IS NOT NULL
  230.  
  231.     ORDER BY
  232.         customerType DESC,
  233.         hashedUserID,
  234.         emailAlertContactsBucket,
  235.         userID DESC
  236.        
  237.  
Add Comment
Please, Sign In to add comment