Advertisement
festinko

Untitled

Nov 18th, 2022
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 5.49 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.  
  156.  
  157. SELECT
  158.  lsptp.userID,
  159.  userEmail,
  160.  uniqueEmailAlertContacts,
  161.  lsptp.customerType,
  162.  lastPaymentProductValue,
  163.  uniqueTeamIntegrationsCount
  164.  
  165.  
  166.  
  167. FROM
  168.   LastSubsPaymentUsersTypeProduct lsptp
  169. LEFT JOIN EmailAlertContacts eac
  170.     ON lsptp.userID = eac.userID
  171. LEFT JOIN `uptimerobot-001.ur.users` u
  172.     ON lsptp.userID = u.userID
  173.  LEFT JOIN countOfTeamIntegrationAlertContactsByUser
  174.     ON lsptp.userID = countOfTeamIntegrationAlertContactsByUser.userID
  175.  
  176.  
  177. WHERE
  178.  lsptp.customerType = 'CurrentCustomer'
  179.  AND uniqueEmailAlertContacts IS NULL
  180.  
  181. ORDER BY
  182.     uniqueEmailAlertContacts DESC
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement