festinko

Untitled

Nov 17th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 9.87 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.        
  90.         FROM `uptimerobot-001.ur.alertcontacts`
  91.         WHERE alertContactType IN (5,7,11,15,17,18,20) AND alertContactStatus = 2
  92.         ),
  93.    
  94.   /*List users with number of unique team integrations alertContacts*/
  95.     countOfTeamIntegrationAlertContactsByUser AS (
  96.       SELECT
  97.         userID,
  98.         COUNT(DISTINCT alertContactValue) as uniqueTeamIntegrationsCount
  99.       FROM
  100.         TeamIntegrationAlertContacts
  101.      
  102.       GROUP BY
  103.         userID
  104.     ),
  105.    
  106. /*List of All Active Not Default E-mail Alert Contacts*/
  107.     NotDefaultEmailAlertContacts AS (
  108.  
  109.         SELECT
  110.             userID,
  111.             alertContactType,
  112.             alertContactValue,
  113.             alertContactStatus,
  114.             rand() as random
  115.         FROM `uptimerobot-001.ur.alertcontacts`
  116.         WHERE
  117.           alertContactType = 2
  118.           AND alertContactStatus = 2
  119.         ),
  120.  
  121. /*Number of Active Unique E-mail Alert Contacts per user*/
  122.     CountOfUniqueEmailAlertContactsPerUser AS(
  123.         SELECT
  124.             userID,
  125.             count(DISTINCT alertContactValue) uniqueEmailAlertContacts
  126.         FROM NotDefaultEmailAlertContacts
  127.         GROUP BY userID
  128.         ORDER BY uniqueEmailAlertContacts DESC
  129.  
  130.     ),
  131.  
  132. /*List of all users with the count of active unique not default Email Alert Contacts by user type - FREE / Current / Churned Customer & productName & productValue*/
  133. EmailAlertContacts AS(
  134.     SELECT
  135.         CountOfUniqueEmailAlertContactsPerUser.userID,
  136.         uniqueEmailAlertContacts,
  137.         (CASE
  138.             WHEN customerType IS NULL THEN 'FREE'
  139.             ELSE customerType
  140.         END) as customerType,
  141.         (CASE
  142.             WHEN lastPaymentProductName IS NULL THEN 'FREE'
  143.             ELSE lastPaymentProductName
  144.         END) as lastPaymentProductName,
  145.         (CASE
  146.             WHEN lastPaymentProductValue IS NULL THEN 0
  147.             ELSE lastPaymentProductValue
  148.         END) as lastPaymentProductValue
  149.        
  150.  
  151.     FROM CountOfUniqueEmailAlertContactsPerUser
  152.     LEFT JOIN LastSuccessSubsPaymentUserTypeProduct
  153.         on CountOfUniqueEmailAlertContactsPerUser.userID = LastSuccessSubsPaymentUserTypeProduct.userID
  154.     ),
  155. /*Number of Users with active Not Default Email AlertContact by customer type and lastPaymentProduct*/
  156. /*
  157. SELECT
  158.     customerType,
  159.     lastPaymentProductName,
  160.     count(userID) as userCount
  161. FROM EmailAlertContacts
  162. GROUP BY
  163.     customerType,
  164.     lastPaymentProductName,
  165.     lastPaymentProductValue
  166. ORDER BY
  167.     customerType,
  168.     lastPaymentProductValue*/
  169.  
  170. preFinalData as (
  171.  
  172.   SELECT
  173.         NotDefaultEmailAlertContacts.userID,
  174.         userEmail,
  175.         MD5(CAST(NotDefaultEmailAlertContacts.userID as string)) hashedUserID,
  176.         DENSE_RANK() OVER (PARTITION BY customerType ORDER BY MD5(CAST(NotDefaultEmailAlertContacts.userID as string))) as rank,
  177.         (CASE
  178.             WHEN
  179.                 userEmail IS NULL THEN '_onlyDefaultEmailAlertContact'
  180.                 ELSE userEmail
  181.             END) userEmailDefault,
  182.         alertContactType,
  183.         alertContactValue,
  184.  
  185.         CONCAT('<li><a href="#" style="color: #3BD771; text-underline: none;">',STRING_AGG(alertContactValue,'</a></li><li><a href="#" style="color: #3BD771; text-underline: none;">' ) OVER (PARTITION BY NotDefaultEmailAlertContacts.userID),'</a></li>') alertContactValueMergedString,
  186.         to_json_string(CONCAT('[',STRING_AGG(alertContactValue,'", "' ) OVER (PARTITION BY NotDefaultEmailAlertContacts.userID),']')) alertContactValueMergedJson,
  187.         (CASE
  188.             WHEN alertContactValue = userEmail THEN 1
  189.             WHEN alertContactValue <> userEmail THEN 0
  190.         ELSE 999999999
  191.         END
  192.         ) isDefaultAlertContact,
  193.         (CASE
  194.             WHEN alertContactValue = userEmail THEN uniqueEmailAlertContacts - 1
  195.             WHEN alertContactValue <> userEmail THEN uniqueEmailAlertContacts
  196.         ELSE 999999999
  197.         END
  198.         ) uniqueEmailAlertContactsCorrected,
  199.         alertContactStatus,
  200.         uniqueEmailAlertContacts,
  201.         (CASE
  202.             WHEN alertContactValue = userEmail AND uniqueEmailAlertContacts <= 4 THEN '<=3'
  203.             WHEN alertContactValue = userEmail AND uniqueEmailAlertContacts >= 5 AND uniqueEmailAlertContacts <= 7 THEN '<4 - 6>'
  204.             WHEN alertContactValue = userEmail AND uniqueEmailAlertContacts >= 8 AND uniqueEmailAlertContacts <= 12 THEN '<7 - 11>'
  205.             WHEN alertContactValue = userEmail AND uniqueEmailAlertContacts >= 13 THEN '>=12'
  206.            
  207.  
  208.             WHEN alertContactValue <> userEmail AND uniqueEmailAlertContacts <= 3 THEN '<=3'
  209.             WHEN alertContactValue <> userEmail AND uniqueEmailAlertContacts >= 4 AND uniqueEmailAlertContacts <= 6 THEN '<4 - 6>'
  210.             WHEN alertContactValue <> userEmail AND uniqueEmailAlertContacts >= 7 AND uniqueEmailAlertContacts <= 11 THEN '<7 - 11>'
  211.             WHEN alertContactValue <> userEmail AND uniqueEmailAlertContacts >= 12 THEN '>=12'
  212.  
  213.             ELSE 'WTF'
  214.         END) emailAlertContactsBucket,
  215.         (CASE
  216.             WHEN customerType IS NULL THEN 'FREE'
  217.             ELSE customerType
  218.         END) as customerType,
  219.         (CASE
  220.             WHEN lastPaymentProductName IS NULL THEN 'FREE'
  221.             ELSE lastPaymentProductName
  222.         END) as lastPaymentProductName,
  223.         (CASE
  224.             WHEN lastPaymentProductValue IS NULL THEN 0
  225.             ELSE lastPaymentProductValue
  226.         END) as lastPaymentProductValue,
  227.         (CASE
  228.             WHEN uniqueTeamIntegrationsCount IS NULL THEN 0
  229.             ELSE uniqueTeamIntegrationsCount
  230.         END) uniqueTeamIntegrationsForFREEusersCount
  231.        
  232.  
  233.     FROM
  234.         NotDefaultEmailAlertContacts
  235.  
  236.     LEFT JOIN
  237.         CountOfUniqueEmailAlertContactsPerUser
  238.     ON NotDefaultEmailAlertContacts.userID = CountOfUniqueEmailAlertContactsPerUser.userID
  239.    
  240.     LEFT JOIN
  241.         `uptimerobot-001.ur.users` u
  242.     ON NotDefaultEmailAlertContacts.userID = u.userID
  243.    
  244.     LEFT JOIN LastSuccessSubsPaymentUserTypeProduct
  245.     ON NotDefaultEmailAlertContacts.userID = LastSuccessSubsPaymentUserTypeProduct.userID
  246.  
  247.     LEFT JOIN countOfTeamIntegrationAlertContactsByUser
  248.     ON NotDefaultEmailAlertContacts.userID = countOfTeamIntegrationAlertContactsByUser.userID
  249.  
  250.    
  251.     WHERE
  252.     NotDefaultEmailAlertContacts.userID IS NOT NULL
  253.     AND  alertContactValue <> userEmail -- filtering only rows where user register email is not equal to additional email Alert Contact
  254.  
  255.     ORDER BY
  256.         customerType DESC,
  257.         hashedUserID,
  258.         emailAlertContactsBucket,
  259.         userID DESC)
  260.  
  261. SELECT
  262.   userID,
  263.   userEmail as email,
  264.   rank,
  265.   alertContactValueMergedString,
  266.   uniqueEmailAlertContactsCorrected,
  267.   emailAlertContactsBucket,
  268.   customerType,
  269.   lastPaymentProductValue,
  270.   uniqueTeamIntegrationsForFREEusersCount
  271.  
  272.  
  273. FROM
  274.   preFinalData
  275.  
  276. WHERE
  277.  customerType = 'FREE'
  278.  AND emailAlertContactsBucket = '<=3'
  279.  AND rank >15000
  280.  
  281.  GROUP BY
  282.   userID,
  283.   userEmail,
  284.   rank,
  285.   alertContactValueMergedString,
  286.   uniqueEmailAlertContactsCorrected,
  287.   emailAlertContactsBucket,
  288.   customerType,
  289.   lastPaymentProductValue,
  290.   uniqueTeamIntegrationsForFREEusersCount
  291.  
  292. ORDER BY rank
Add Comment
Please, Sign In to add comment