SQL in BigQuery - How to loop through every month in a timeframe and calculate some metrics (i.e. count userID) - sql

I am doing a test to calculate user retention for a bank. While doing so, I can certainly calculate the number of churn users and number of loyal users for a certain date period (for example, from 1 jan to 1 feb). However, the timeframe has every transactions/activities of every unique user from 1 jan 2016 to 1 jun 2018. I know we could do a loop in sql to automatically loop through every month but it is hard to do so with the calculation (with new users joining every month and the new users of month 1 would either be loyal user or churn in month 2).
Could anybody shed a light for me?

Related

Calculate the number of active vendor in the last 3 months in Bigquery

I am a newcomer to SQL, I want to calculate the number of active vendors in the last 3 months (A vendor is considered to be active if there is at least one transaction with that vendor within a
certain timeframe)
The data getting from access the https://console.cloud.google.com/marketplace/product/iowa-department-of-commerce/iowa-liquor-sales?filter=category:analytics&filter=price:free&filter=solution-type:dataset&project=fiery-plate-322918&folder=&organizationId= dataset.
Thank you in advance <3
The table was updated on Jun 4, 2019. So there are no current data available of the last months.
vendor_number
FROM `bigquery-public-data.iowa_liquor_sales.sales`
where date>date_sub (current_date(),interval 3 month)
GROUP BY 1
returns no data.

I need to write an .sql query that shows data aggregated at the monthly level looking back over the past two years

Right now the problem with my code is that it counts each month as a new aggregation, meaning that January and February will contain activity for the same account in both months. I only need the monthly activity for each month at the level of each account.
Right now where for example the activity for Account A = 100 for January and activity for Account A = 25 in February, my query shows activity in February for account A as 125 when I would like it to be returned as A = 100 for January and A = 25 for February.
Is this clear? If I use for example (YEAR(date_column)) the aggregation at the annual level is accurately represented. This approach however does not allow me to provide a monthly breakdown of the data. If I add a second column to get the "Month" values from the date e.g: (MONTH(date_column)), the numbers are aggregated month over month and not at the month level alone.

Data warehouse, SSAS

I have been given population data like this;
Year Region Population
----------------------------------
2012 District1 1000
2012 District2 1500
2012 District3 2000
Now I have to make a cube where a user can filter population in Month, Quarter and Year level. So I decided to enter data into a fact table with each and every month of the given year that means 12 records for each District with the same given number. So if a user asks for any month he will get the same count. But now the problem is if user does not filter it by Month, Quarter and Year I get the Sum of all the data that means District1 will display 12 times 1000 = 12000. How can I get 1000 for district1 at any given time? If data is in multiple years then also it should not sum them up. Is my approach wrong? Hope I am clear enough to explain the problem.
Your fact is a semi additive measure, more info at http://msdn.microsoft.com/en-us/library/ms175356.aspx

extracting common data of current months and last two monnth sql

I have a table with more than 20000 rows, In one of column i have month from jan 2014 to Dec 2014, and in another column i have a loan number. Most of the loan Numbers are reapeting every months,now i need to get only the loan Number which are apperead in all three monthy consecutively. For eg if i am getting data for current months i also wanted get data which are common in two months before the current months. The database that i m using is Access DB. Any adivice will be more than a help, Thanks in Advance.
SELECT Loans.LoanID, Sum(IIf([period]=[month],1,0)) AS CM, Sum(IIf([period]=[month]-1,1,0)) AS [m-1], Sum(IIf([period]=[month]-2,1,0)) AS [m-2]
FROM Loans
GROUP BY Loans.LoanID
HAVING (((Sum(IIf([period]=[month],1,0)))>1) AND ((Sum(IIf([period]=[month]-1,1,0)))>1) AND ((Sum(IIf([period]=[month]-2,1,0)))>1));
I used month as an integer, and didn't make any adjustment for months 1 and 2 to loop back and look at prior year - you should be able to modify this based on the actual format you are using for the month.

Performing MTD/YTD calcs over multiple calendars in analysis services

I have the following situation in my cube:
Shop A uses calendar Cal1. Their sales month starts Jan 5th.
Shop B uses calendar Cal2. Their sales month starts Jan 10th.
Shop C...etc
Shop calendars can not simply be represented as offsets of a main calendar. They have different working days, public holidays etc.
I need to produce a daily (reporting services) report with the actual calendar date as a parameter. The list of shops is also a multi select parameter. If a user selects the 15th of Jan, I need to show the combined MTD sales for all shops selected in the parameters. So that would mean the first 10 days of sales for shop A and the first 5 days of sales for shop B etc.
Any ideas how I can make this work? I'll also need to provide YTD figures in the same manner.
I am implementing multiple calendars using a bridging table between my date and calendar dimensions. It is the technique described here: http://duncansutcliffe.wordpress.com/2010/06/11/a-better-date-dimension/
I can not hard code the calendars as there is a requirement to possibly add more in the future without modifying the schema.
I am not sure I understand you sales data start days, but if I do then the solution is to make an extra dimension as a "reporting calendar" as a point of harmony between the actual calendars
Each shop has a known offset to the reporting calendar, so for shop A it's 5 days, for shop B it's 10 days etc
When you add fact data you also need to calculate a reporting date using the offset. So for Shop A 5 Jan is actually 1 Jan etc
When reporting, the user selects a date on the reporting calendar, and facts are selected based on that
e.g. if the user selected reporting calendar 15th Jan, it would only select actual dates 1 to 15 Jan and reporting calendar up to 15 and only Jan
Data selected would be Shop A 5 to 15, Shop B 10 to 15
1 to 4 Jan for Shop A and 1 to 9 for Shop B would be in Dec of the reporting calendar, and not included because of the filter of reporting calendar Jan