Advertisement
Guest User

Untitled

a guest
May 5th, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. -- month aggregate data
  2. -- purchases in the month
  3.  
  4. -- net new connector report
  5. select companies.name, companies.email, connectors.name as "connector", company_connectors.created_at from company_connectors
  6. join companies on company_connectors.company_id = companies.id
  7. join connectors on connectors.id = company_connectors.connector_id
  8. join summaries on summaries.company_id=company_connectors.company_id
  9. where
  10. company_connectors.created_at > '05/01/2016'
  11. and company_connectors.deleted_at ISNULL
  12. and company_connectors.synced = TRUE
  13. and email NOT LIKE '%@test.%' and email NOT LIKE '%avalara%'
  14. GROUP BY companies.name, companies.email, connectors.name, company_connectors.created_at;
  15.  
  16.  
  17. -- punt report
  18.  
  19.  
  20. select usage.company_name, merchants.email,
  21. (case when usage.connector_name = 'xero' then 1 else 0 end) AS "xero",
  22. (case when usage.connector_name = 'amazon' then 1 else 0 end) AS "amazon",
  23. (case when usage.connector_name = 'shopify' then 1 else 0 end) AS "shopify",
  24. (case when usage.connector_name = 'weebly' then 1 else 0 end) AS "weebly",
  25. (case when usage.connector_name = 'freshbooks' then 1 else 0 end) AS "freshbooks",
  26. (case when usage.connector_name = 'etsyv2' then 1 else 0 end) AS "etsyv2",
  27. (case when usage.connector_name = 'qbo' then 1 else 0 end) AS "qbo",
  28. (case when usage.connector_name = 'prestashop' then 1 else 0 end) AS "prestashop",
  29. sum(subscriptions.total_price) as "purchased?"
  30. --(case when subscriptions.id NOTNULL and subscriptions.total_price > 0 then true else false end) as "purchased"
  31.  
  32. FROM dblink('dbname=trustfilestorage user=deploy password=4BFkh59jDd9YCSu',
  33. 'SELECT account_id, company_name, connector_name, created_at from company_daily_usage
  34. where created_at > ''05/01/2016''
  35. and usage = ''CONNECTOR_ACTIVE''
  36. ')
  37. AS usage(account_id INT, company_name TEXT, connector_name TEXT, usage_date TIMESTAMP)
  38. JOIN merchants on merchants.account_id = usage.account_id
  39. LEFT OUTER JOIN subscriptions on subscriptions.account_id = merchants.account_id
  40. where email NOT LIKE '%test.%' and email NOT LIKE '%avalara%' and email NOT LIKE '%sush.io%'
  41. GROUP BY usage.company_name, merchants.email, usage.connector_name, subscriptions.total_price, subscriptions.id
  42. ;
  43.  
  44.  
  45.  
  46. -- connector usage report
  47.  
  48.  
  49.  
  50. select usage.company_name, merchants.email,
  51. (case when usage.connector_name = 'xero' then 1 else 0 end) AS "xero",
  52. (case when usage.connector_name = 'amazon' then 1 else 0 end) AS "amazon",
  53. (case when usage.connector_name = 'shopify' then 1 else 0 end) AS "shopify",
  54. (case when usage.connector_name = 'weebly' then 1 else 0 end) AS "weebly",
  55. (case when usage.connector_name = 'freshbooks' then 1 else 0 end) AS "freshbooks",
  56. (case when usage.connector_name = 'etsyv2' then 1 else 0 end) AS "etsyv2",
  57. (case when usage.connector_name = 'qbo' then 1 else 0 end) AS "qbo",
  58. (case when usage.connector_name = 'prestashop' then 1 else 0 end) AS "prestashop",
  59. -- (case when subscriptions.created_at > '05/01/2016' then subscriptions.total_price else 0 end)
  60.  
  61. --sum( subscriptions.total_price) as "purchases in the month"
  62. FROM dblink('dbname=trustfilestorage user=deploy password=4BFkh59jDd9YCSu',
  63. 'SELECT account_id, company_name, connector_name, created_at from company_daily_usage
  64. where created_at > CURRENT_DATE - INTERVAL ''8 days''
  65. and usage = ''CONNECTOR_ACTIVE''
  66. ')
  67. AS usage(account_id INT, company_name TEXT, connector_name TEXT, usage_date TIMESTAMP)
  68. JOIN merchants on merchants.account_id = usage.account_id
  69. INNER JOIN (select companies.account_id,
  70. sum (case when (subscriptions.created_at > '05/01/2016'
  71. and (subscriptions.expired_at ISNULL OR subscriptions.expired_at > now())
  72. and (subscriptions.revoked_at ISNULL and subscriptions.cancelled_at ISNULL ))
  73. then subscriptions.total_price else 0 end) AS "$"
  74. from companies
  75. LEFT OUTER JOIN subscriptions on subscriptions.account_id = companies.account_id
  76. GROUP BY companies.account_id) purchases
  77. on (purchases.account_id = usage.account_id)
  78. where email NOT LIKE '%test.%' and email NOT LIKE '%avalara%' and email NOT LIKE '%sush.io%'
  79.  
  80. GROUP BY usage.company_name, merchants.email, usage.connector_name
  81. ;
  82.  
  83.  
  84. -- recent purchases by account_id
  85. select companies.account_id,
  86. sum (case when (subscriptions.created_at > '05/01/2016'
  87. and (subscriptions.expired_at ISNULL OR subscriptions.expired_at > now())
  88. and (subscriptions.revoked_at ISNULL and subscriptions.cancelled_at ISNULL ))
  89. then subscriptions.total_price else 0 end) AS "subscription"
  90. from companies
  91. LEFT OUTER JOIN
  92. subscriptions on subscriptions.account_id = companies.account_id
  93. --where
  94. -- (subscriptions.expired_at ISNULL OR subscriptions.expired_at > now())
  95. -- and (subscriptions.revoked_at ISNULL and subscriptions.cancelled_at ISNULL )
  96. -- and subscriptions.created_at > '05/01/2016'
  97. GROUP BY companies.account_id;
  98.  
  99.  
  100. select id, account_id from companies where name = 'Maurice Cole Surfboards USA'; -- 9169 8294
  101.  
  102. select * from subscriptions where account_id = 8294
  103.  
  104.  
  105.  
  106. -- original connector usage report
  107. select usage.company_name, merchants.email, usage.connector_name, sum( subscriptions.total_price) as "purchases in the month"
  108. FROM dblink('dbname=trustfilestorage user=deploy password=4BFkh59jDd9YCSu',
  109. 'SELECT account_id, company_name, connector_name, created_at from company_daily_usage
  110. where created_at > CURRENT_DATE - INTERVAL ''8 days''
  111. and usage = ''CONNECTOR_ACTIVE''
  112. ')
  113. AS usage(account_id INT, company_name TEXT, connector_name TEXT, usage_date TIMESTAMP)
  114. JOIN merchants on merchants.account_id = usage.account_id
  115. LEFT OUTER JOIN subscriptions on subscriptions.account_id = usage.account_id
  116. where email NOT LIKE '%test.%' and email NOT LIKE '%avalara%'
  117. and (subscriptions.expired_at ISNULL OR subscriptions.expired_at > now())
  118. and (subscriptions.revoked_at ISNULL and subscriptions.cancelled_at ISNULL )
  119. --and (subscriptions.created_at > CURRENT_DATE - INTERVAL '8 days' or subscriptions.id ISNULL )
  120.  
  121. GROUP BY usage.company_name, usage.connector_name, merchants.email
  122. ;
  123.  
  124.  
  125. select id, account_id from companies where name = 'Maurice Cole Surfboards USA'; -- 9169 8294
  126.  
  127. select * from subscriptions where account_id = 8294
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement