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

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.

Related

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

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?

Add results to a row regarding the last 12 months rows- SQL Server

At my last meeting someone asked me if it was possible to hide people who where ill since a year from a dashboard. So I'm searching for the best way to actually KNOW who has been ill for 12 months.
I am working with a table with the number of days you've been absent for every kind of absence you could have, the number of days you should have been working that month, with a row per person, department and profession each month.
So it looks something like this :
PersonID
YEAR
MONTH
DEPARTMENT
PROFESSION
Absence1
Absence2
Absence3
WORKING DAYS OF THE MONTH...
11111
2021
07
HR
ASSISTANT
0
2
0
22
11111
2021
08
HR
ASSISTANT
0
0
0
22
==> So if I'm on a row of July 2021 I need to check the lines from June 2020 to June 2021.
My guess is that I need to add a column to this table who will say (with some kind of loop maybe) "if for the last 12 months (rows) the total number of days of absence equal the number of working days of the last 12 months then "ILL FOR A YEAR OR MORE" for each person (knowing that a person can work in more than one department or more than one profession so she'll have more than one row per month).
But I really have no idea how to actually write it in a script as I usually do very basic things. I'm using SQL SERVER and have 429 207 rows in the table. I'm thinking about doing it in the whole table and not only treating this month's rows because in the dashboard we show an historic.
Your table is heavily denormalized. If you want to represent all this information in the database, I would have expected the following tables, instead of just one:
Person
Department
Illness (list of illnesses)
IllnessAbsence (join table between Person and Illness)
Either way, you can get the information you need with something like this:
I've assumed you want the whole table, so you need a window function
We need to flip the logic on its head: exclude all rows which have no non-absence in the last 12 months
SELECT
PersonID,
YEAR,
MONTH,
DEPARTMENT,
PROFESSION,
ILLNESS1,
ILLNESS2,
ILLNESS3,
[WORKING DAYS OF THE MONTH]
FROM (
SELECT *,
NotIllLast12Months = COUNT(CASE WHEN DATEFROMPARTS(YEAR + 1, MONTH, 1) >= GETDATE()
AND ILLNESS1 + ILLNESS2 + ILLNESS3 = 0 THEN 1 END)
OVER (PARTITION BY ID)
FROM HETP_ABS
) abs
WHERE NotIllLast12Months > 0;

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.

rearrange SQL data to different view

I have a database table with product, salesvolume, date and a timestamp in SQL server.
I have data in tables as:
date product time volume
10-28-2014 a 15:05 5
10-28-2014 a 16:06 8
10-28-2014 b 9:05 3
Required format:
date product h10 h16 h17
"10-28-2014" a 5 8
"10-28-2014" b 3
From this I want to create a new table/view in which I have total sales per day, per product, per hour of the day.
So i'll have 24 columns, 1 per hour of the day and one record for every day for every product.
Is this possible to create with just SQL? any tips how to do this?
Thank you!
I'm sorry but can't get the table layout to work...
You want to first of all group your data by day, hour (which you can extract from your time-stamp field), product and sum by sales.
From this you can then use the pivot function to cross-tab your hour field - you'll find plenty of documentation about the pivot functionality on-line.

KPI indicator for QlikView report update status. (Weekly and monthly updated)

I have two reports, one that is updated monthly from the source, and one that is updated once per week.
I have a KPI report that I want to show red/green indicators if the report is updated this month/this week.
The last updated date is stored in a table and is loaded into QlikView. QVD_Id is the id of the QV report.
The monthly report is beeing updated around the 10th each month. I have created this set analysis that seems to be "almost" working.
=if(Max({<Day=, Date=, QVD_Id={1}, MonthCounter={"<=$(#max(MonthCounter))"}>} num(Date(Updated))-1,00) >= num(Dato),0,1)
What I ultematly want for this indicator, is for it to be red if has not been updated within a month, or actually if its not updated within the 6th working day of the month it should be red(But this requered som additions to the dimensions that I can do later).
I tried to do the same for the weekly report, but its not working:
=if(Max({<Day=, Date=, QVD_Id={2}, WeekCounter={"<=$(#max(WeekCounter))"}>} num(Date(Updated))) >= num(Dato),0,1)
What I want with the weekly updated report, is that it should be red if it's not updated by the 2nd day of the week(Tuesday).
Example of the facttable used:
QVD_Id TotalLoad LoadYear LoadMonth LoadDay LoadHour Updated Date_Lnr Source
1 200000 2014 2 5 10 .02.2014 10:56:31 19759 Source1
WeekCounter is 58 this week, and MonthCounter is 2. (WeekCounter is just continiuing from last year but that does not matter here).
Hope any of you have some more experience then me with this and can find a solution for this.
i think this is close to what you want:
// check is date less than the 6 in the month and not larger than max date -1 month
if(num(weekday(Max(Date))) <= 6 and
num(weekday(AddMonth(Max(Date),-1))) <= num(weekday(Max({$}Updated))), 1,0)