I'm doing some work around what we're spending on support vs. how much those users bring in and came into this unique problem.
Tables I have:
Revenue table: A row for each time a user generates revenue on the platform
Support Contacts Table: A row for each time a user contacts support and the cost associated with that contact
I'm trying to get a table at a daily grain that details...
How many users contacted support on the given day
How much revenue did all users bring in in the last 30 days?
How much did we spend on support contacts in the last 30 days?
The tough part: How much did the users who contacted support on the given day bring in in the last 30 days?
Here's what I have so far:
SELECT DISTINCT
-- Revenue generation date
r.revenue_date
-- Easy summing of contacts/revenue/costs on the given day
,COUNT(DISTINCT sc.user_pk) AS num_user_contacting_support_on_day
,SUM(r.revenue) AS all_users_revenue_for_day
,SUM(sc.support_contact_cost) AS support_costs_on_day
-- Double check that this would sum the p30d revenue/costs for the given day?
,SUM(IF(r.revenue_date BETWEEN r.revenue_date AND DATE_SUB(r.revenue_date, INTERVAL 30 DAY), c.revenue, NULL)) AS p30d_revenue_all_users
,SUM(IF(sc.support_contact_date BETWEEN r.revenue_date AND DATE_SUB(r.revenue_date, INTERVAL 30 DAY), sc.support_contact_cost, NULL)) AS p30d_support_contact_cost
-- The tough part:
-- How do I get the revenue for ONLY users who contacted support on the given day?
-- How do I get the p30d revenue for ONLY users who contacted support on the given day?
FROM revenue_table r
LEFT JOIN support_contact_table sc
ON r.revenue_date = sc.support_contact_date
GROUP BY r.revenue_date
Related
I'm trying to solve this task. But I can't understand how to add 3 months condition right. Please help
Partners receive bonuses for every client that they introduce to the company. Assume that the company has 3 active partners.
Partner paid on a monthly based - for every introduced client partner gets 0.5% of the amount deposited by the client excluding withdrawals. But, with the condition that if the client withdraws the amount more than 3 months after the amount was deposited – such withdrawals are not included in the partner’s bonus reduction.
For example, in August partner’s client deposited $1000 and withdraw $100. Partner gets (1000-100)*0.5%=$4.5. But in case the $100 withdrawal is from the deposit made by the client in April, in this case, the partner’s bonus is (1000)*0.5%=$5
Task: write a SQL script that calculates the monthly bonus amount for each partner.
Data is in the link [1]: https://docs.google.com/spreadsheets/d/17xo_gnu9nEwXuCNb8vBLEVrhPEKJaIRN/edit?usp=sharing&ouid=112201519964034772995&rtpof=true&sd=true
Between worls only when the date left of the AND is lower tha the right.
So your code mus look like
select
*
from
transactions
where date=any (select
date
from
transactions
where type='withdrawal'
and date between (date - interval '3 month') AND date)
and type='deposit'
order by 1,3,7
I'm new to SQL and I'm struggling with few of the questions on my exercise. How to calculate Day 7 retention rate of all players acquired on 01 October 2019. The tables shown is just a sample of what the extended table is.
My answer was:
SELECT
Session_table,
COUNT(DISTINCT user_id) as active_users,
COUNT(DISTINCT future_ user_id) as retained_users,
CAST(COUNT(DISTINCT future_ user_id) / COUNT(DISTINCT user_id) AS float) retention
FROM inapp_purchase
LEFT JOIN user_id as future_id
ON user_id = future_ user_id
AND user_id. inapp_purchase = user_id.session_table - datetime(page_views.pv_ts, '+7 day')
GROUP BY 1
There are two tables:
session_table
When each user_id starts a session , the session will have a unique session_id. Session_length_seconds tells how long a session lasted in seconds. level tells what game level the player was at the end of that session. The session_table will have one line for each session each user has (users can have multiple sessions per day each with unique session_id).
inapp_purchase
The inapp_purchase table has one line for each product that the user (denoted by user_id) purchased. There can be multiple purchases in a day per user, per session. The session_id and user_id here can link to the session_table to track active users who also make a payment. Product_id tells which product was purchased and the purchase_value tells the amount the user paid in $.
There are also three requests for the inapp_purchase table:
Calculate daily average revenue per daily active user for last 3 months
Calculate the daily average revenue per paying user year to date?
Calculate the daily conversion rate over the last month? This measure is defined by the proportion of daily active users who make a purchase on the day in question.
Please let me know if the above information is sufficient.
Thank you for your help.
Given ~23 million users, what is the most efficient way to compute the cumulative number of logins within the last X months for any given day (even when no login was performed) ? Start date of a customer is its first ever login, end date is today.
Desired output
c_id day nb_logins_past_6_months
----------------------------------------------
1 2019-01-01 10
1 2019-01-02 10
1 2019-01-03 9
...
1 today 5
➔ One line per user per day with the number of logins between current day and 179 days in the past
Approach 1
1. Cross join each customer ID with calendar table
2. Left join on login table on day
3. Compute window function (i.e. `sum(nb_logins) over (partition by c_id order by day rows between 179 preceding and current row)`)
+ Easy to understand and mantain
- Really heavy, quite impossible to run on daily basis
- Incremental does not bring much benefit : still have to go 179 days in the past
Approach 2
1. Cross join each customer ID with calendar table
2. Left join on login table on day between today and 179 days in the past
3. Group by customer ID and day to get nb logins within 179 days
+ Easier to do incremental
- Table at step 2 is exceeding 300 billion rows
What is the common way to deal with this knowing this is not the only use case, we have to compute other columns like this (nb logins in the past 12 months etc.)
In standard SQL, you would use:
select l.*,
count(*) over (partition by customerid
order by login_date
range between interval '6 month' preceding and current row
) as num_logins_180day
from logins l;
This assumes that the logins table has a date of the login with no time component.
I see no reason to multiply 23 million users by 180 days to generate a result set in excess of 4 million rows to answer this question.
For performance, don't do the entire task all at once. Instead, gather subtotals at the end of each month (or day or whatever makes sense for your data). Then SUM up the subtotals to provide the 'report'.
More discussion (with a focus on MySQL): http://mysql.rjweb.org/doc.php/summarytables
(You should tag questions with the specific product; different products have different syntax/capability/performance/etc.)
I want to pull Emergency room visits before a members first treatment date. Everyone as a different first treatment date and none occur before Jan 01 2012.
So if a member has a first treatment date of Feb 24 2013, I want to know how many times they visited the ER one year prior to that date.
These min dates are located in another table and I can not use the Min date in my DATEADD function. Thoughts?
One possible solution is to use a CTE to capture the visits between the dates your interested in and then join to that with your select.
Here is an example:
Rextester
Edit:
I just completely updated my answer. Sorry for the confusion.
So you have at least two tables:
Emergency room visits
Treatment information
Let's call these two tables [ERVisits] and [Treatments].
I suppose both tables have some id-field for the patient/member. Let's call it [MemberId].
How about this conceptual query:
WITH [FirstTreatments] AS
(
SELECT [MemberId], MIN([TreatmentDate]) AS [FirstTreatmentDate]
FROM [Treatments]
GROUP BY [MemberId]
)
SELECT V.[MemberId], T.[FirstTreatmentDate], COUNT(*) AS [ERVisitCount]
FROM [ERVisits] AS V INNER JOIN [FirstTreatments] AS T ON T.[MemberId] = V.[MemberId]
WHERE DATEDIFF(DAY, V.[VisitDate], T.[FirstTreatmentDate]) BETWEEN 0 AND 365
GROUP BY V.[MemberId], T.[FirstTreatmentDate]
This query should show the number of times a patient/member has visited the ER in the year before his/her first treatment date.
Here is a tester: https://rextester.com/UXIE4263
I'm quite new to access and I am currently in the process of making a database for my company.
I have a 'Jobs' table with these fields in:
Job No.
Year Initiated
Month Initiated
Company ID
Job Description
Amount Quoted
Amount to Invoice
Invoice Number
Completed By
Cost
Profit
What I want to know Is what is the best way/ how do I calculate either in a form or query the overall profit for each month?
Please help, the database is really coming along, apart from this is well entruely stuck on.
You want to find all rows matching a specific year / month, and add together all the profit entries for that month to get a total?
If so, try this :
select sum(profit) from Jobs where year = 2013 and month = 02
Or, if you want to retrieve this information for all months in one go, try this :
select year, month, sum(profit) from Jobs group by year, month