Advertisement
festinko

Untitled

Dec 22nd, 2022
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.09 KB | None | 0 0
  1. WITH newSubs as (
  2.  
  3. /*New Subsscriptions*/
  4. SELECT
  5.     DATE(paymentDateTime) firstSubsPaymentDate,
  6.   COUNT(DISTINCT userID) firstSubscriptionUsersCount_G4,
  7.     ROUND(SUM(paymentAmount)) firstSubscriptionRevenue,
  8.     ROUND(SUM(paymentAmount/paymentPeriod)) firstSubscriptionMRR
  9.  
  10. FROM
  11.   payments
  12.  
  13. WHERE
  14.   paymentNewUpgradeRenewal = 1
  15.   AND paymentPeriod IN (1,12)
  16.     AND paymentStatus = 1
  17.   AND userID IS NOT NULL
  18.  
  19. GROUP BY
  20.     firstSubsPaymentDate
  21.    
  22.  
  23. ORDER BY
  24.     firstSubsPaymentDate DESC),
  25.    
  26. newSigunps as (
  27.         SELECT
  28.             DATE(userRegisterDateTime) registerDate,
  29.             count(DISTINCT userID) newSigunUpsVerified_G2
  30.        
  31.         FROM
  32.             users
  33.        
  34.         WHERE
  35.             userStatus = 1
  36.             AND userID IS NOT NULL
  37.        
  38.         GROUP BY
  39.             registerDate
  40.     ),
  41.    
  42.     upgradedSubs as (
  43.     /*Upgraded Subsscriptions*/
  44.  
  45. SELECT
  46.     DATE(paymentDateTime) upgradedSubsPaymentDate,
  47.   COUNT(DISTINCT userID) upgradedSubscriptionUsersCount,
  48.     ROUND(SUM(paymentAmount)) upgradedSubscriptionRevenue,
  49.     ROUND(SUM(paymentAmount/paymentPeriod)) upgradedSubscriptionMRR
  50.  
  51. FROM
  52.   payments
  53.  
  54. WHERE
  55.   paymentNewUpgradeRenewal = 2
  56.   AND paymentPeriod IN (1,12)
  57.     AND paymentStatus = 1
  58.   AND userID IS NOT NULL
  59.  
  60. GROUP BY
  61.     upgradedSubsPaymentDate
  62.    
  63.  
  64. ORDER BY
  65.     upgradedSubsPaymentDate DESC
  66.     ),
  67.    
  68.     renewedSubs as (
  69.     /*Renewed Subsscriptions*/
  70.  
  71. SELECT
  72.     DATE(paymentDateTime) renewedSubsPaymentDate,
  73.   COUNT(DISTINCT userID) renewedSubscriptionUsersCount,
  74.     ROUND(SUM(paymentAmount)) renewedSubscriptionRevenue,
  75.     ROUND(SUM(paymentAmount/paymentPeriod)) renewedSubscriptionMRR
  76.  
  77. FROM
  78.   payments
  79.  
  80. WHERE
  81.   paymentNewUpgradeRenewal = 3
  82.   AND paymentPeriod IN (1,12)
  83.     AND paymentStatus = 1
  84.   AND userID IS NOT NULL
  85.  
  86. GROUP BY
  87.     renewedSubsPaymentDate
  88.    
  89.  
  90. ORDER BY
  91.     renewedSubsPaymentDate DESC
  92.     ),
  93.    
  94.     canceledSubs AS (
  95.         SELECT
  96.             DATE(cancelled) cancellDate,
  97.             count(userID) canceledSubsCount
  98.  
  99.         FROM
  100.             subscriptions
  101.  
  102.         GROUP BY
  103.             cancellDate
  104.  
  105.         ORDER BY
  106.             cancellDate DESC),
  107.            
  108.    
  109.     subusersCreated as
  110.     (SELECT
  111.         DATE(created) subuserCreatedDate,
  112.         COUNT(userID) subusersCreatedCount
  113.  
  114.     FROM
  115.         organization_members
  116.        
  117.     GROUP BY
  118.             subuserCreatedDate
  119.     ORDER BY
  120.         subuserCreatedDate DESC),
  121.        
  122.     usersFirstAddonPayment as (
  123.  
  124. /*First users payment for add on*/
  125. SELECT
  126.     userID,
  127.     MIN(DATE(paymentDateTime)) firstSubuserPaymentDate
  128. FROM
  129.     payments
  130.  
  131. WHERE
  132.  
  133.   paymentType = 3
  134.     AND productID = 2
  135.     AND paymentDateTime > '2022-12-11'
  136.  
  137. GROUP BY
  138.     userID
  139.  
  140. ORDER BY
  141.   firstSubuserPaymentDate DESC),
  142.  
  143. firstAddonPayment as (
  144.     /*Count of users with first payment for sub user by date*/
  145.     SELECT
  146.         firstSubuserPaymentDate,
  147.         count(DISTINCT userID) firstSubuserPaymentUsersCount_G8
  148.    
  149.     FROM
  150.         usersFirstAddonPayment
  151.    
  152.     GROUP BY
  153.         firstSubuserPaymentDate
  154.    
  155.     ORDER BY
  156.         firstSubuserPaymentDate DESC),
  157.  
  158. addOnCreatedDaily as (
  159.  
  160. /*Daily Number of additional subusers bought*/
  161. SELECT
  162.     DATE(created) as firstAddOnCreatedDate,
  163.     SUM(`limit`) addOnCreatedSum
  164. FROM
  165.     subscription_addons
  166.  
  167. GROUP BY
  168.     firstAddOnCreatedDate
  169.  
  170. ORDER BY
  171.     firstAddOnCreatedDate DESC)
  172.  
  173.  
  174. SELECT
  175.     registerDate as date,
  176.     newSigunUpsVerified_G2,
  177.     firstSubscriptionUsersCount_G4,
  178.     (firstSubscriptionUsersCount_G4/newSigunUpsVerified_G2) as crG2toG4,
  179.     firstSubscriptionRevenue,
  180.     firstSubscriptionMRR,
  181.     upgradedSubscriptionUsersCount,
  182.     upgradedSubscriptionRevenue,
  183.     upgradedSubscriptionMRR,
  184.     renewedSubscriptionUsersCount,
  185.     renewedSubscriptionRevenue,
  186.     renewedSubscriptionMRR,
  187.     canceledSubsCount,
  188.     subusersCreatedCount,
  189.     addOnCreatedSum
  190.    
  191.    
  192.    
  193. FROM
  194.     newSigunps
  195.  
  196. LEFT JOIN
  197.     newSubs
  198.  
  199. ON newSigunps.registerDate = newSubs.firstSubsPaymentDate
  200.  
  201. LEFT JOIN
  202.     upgradedSubs
  203.  
  204. ON newSigunps.registerDate = upgradedSubs.upgradedSubsPaymentDate
  205.  
  206. LEFT JOIN
  207.     renewedSubs
  208.  
  209. ON newSigunps.registerDate = renewedSubs.renewedSubsPaymentDate
  210.  
  211. LEFT JOIN
  212.     canceledSubs
  213.  
  214. ON
  215.     newSigunps.registerDate = canceledSubs.cancellDate
  216.    
  217. LEFT JOIN
  218.     subusersCreated
  219.    
  220. ON
  221.     newSigunps.registerDate = subusersCreated.subuserCreatedDate
  222.    
  223. LEFT JOIN
  224.     addOnCreatedDaily
  225.  
  226. ON
  227.     newSigunps.registerDate = addOnCreatedDaily.firstAddOnCreatedDate
  228.  
  229. ORDER BY
  230.     date DESC
  231.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement