How to find the first consecutive days of a dataset in SQL? - presto

Im trying to select only the first consecutive dates in SQL from the table
The final results should look like this
This is the example of the initial table.
This is the initial table I have.
> Campaign Code Current Date Campaign Start Date Days Since Campaign
> Launch Next DaTe Next date - Current Date domestic campaign
> 1 10/01/2022 10/01/2022 0 11/01/2022 1 domestic campaign
> 1 11/01/2022 10/01/2022 1 12/01/2022 1 domestic campaign
> 1 12/01/2022 10/01/2022 2 13/01/2022 1 domestic campaign
> 1 13/01/2022 10/01/2022 3 14/01/2022 1 domestic campaign
> 1 14/01/2022 10/01/2022 4 15/01/2022 1 domestic campaign
> 1 15/01/2022 10/01/2022 5 16/01/2022 1 domestic campaign
> 1 16/01/2022 10/01/2022 6 17/01/2022 1 domestic campaign
> 1 17/01/2022 10/01/2022 7 18/01/2022 1 domestic campaign
> 1 18/01/2022 10/01/2022 8 19/01/2022 1 domestic campaign
> 1 19/01/2022 10/01/2022 9 30/01/2022 11 domestic campaign
> 1 30/01/2022 10/01/2022 20 31/01/2022 1 domestic campaign
> 1 31/01/2022 10/01/2022 21 01/02/2022 1 domestic campaign
> 1 01/02/2022 10/01/2022 22 19/05/2022 107 domestic campaign
> 1 19/05/2022 10/01/2022 129 20/05/2022 1
And I am looking to select only the first consecutive dates which would return the following:
Campaign Code Current Date Campaign Start Date Days Since Campaign Launch Next DaTe Next date - Current Date
domestic campaign 1 44571 44571 0 44572 1
domestic campaign 1 44572 44571 1 44573 1
domestic campaign 1 44573 44571 2 44574 1
domestic campaign 1 44574 44571 3 44575 1
domestic campaign 1 44575 44571 4 44576 1
domestic campaign 1 44576 44571 5 44577 1
domestic campaign 1 44577 44571 6 44578 1
domestic campaign 1 44578 44571 7 44579 1
domestic campaign 1 44579 44571 8 44580 1
Any help is much appreciated.

You can use ROW_NUMBER function subtracted from 'Next Date' to create groups for consecutive dates per campaign code, then use DENSE_RANK function to select only the first consecutive group for each campaign code.
With get_groups As
(
Select *,
Date_add('day', -ROW_NUMBER() Over (Partition By CampaignCode Order By NextDate), NextDate) As grp
From table_name
),
get_ranks As
(
Select *,
DENSE_RANK() OVER (Partition By CampaignCode Order By grp) As rnk
From get_groups
)
Select CampaignCode, CurrentDate, CampaignStartDate,
DaysSinceCampaign, NextDate, Nextdate_CurrentDate
From get_ranks
Where rnk = 1;
You may try to google "gaps and islands problem in SQL" for more details about this problem.
Side Note: ('Days Since Campaign', 'Campaign Start Date' and 'Next date - Current Date') can be easily calculated from other columns, so you don't need to store them in the table.

Related

Can i use TextSplit with Find formula?

I am making a table up that will sum all matches of a company found within a specific time period. I need to also exclude certain months if they are inserted into a cell as mm/yy. Excluding one month is fine but when i type 10/22, 11/22, it will sum everthing. THe below code is what i am using with U$4 being the end of a month minus the tracking period which is 90 days. Note that the Raw Data that it is reading from only goes to end of November.
=IF([#[Company Name]]="","",SUM(IF(ISNUMBER(SEARCH([#[Company Name]],RawData[Description]))=TRUE,IF(RawData[Home]=XLOOKUP($D$1,HomeList[Home Code],HomeList[Home]),IF(RawData[Source]="Spend Money",IF(RawData[Date]<=U$4,IF(RawData[Date]>=U$4-[#[Tracking period (Days)]],1,0)))))))
With one date inserted which is correct:
28/Feb 31/Mar 30/Apr 31/May 30/Jun 31/Jul 31/Aug 30/Sep 31/Oct 30/Nov 31/Dec 31/Jan
Exclude Company Name Tracking period (Days) Month 1 Month 2 Month 3 Month 4 Month 5 Month 6 Month 7 Month 8 Month 9 Month 10 Month 11 Month 12
11/22 CLH 90 0 0 0 0 0 0 0 1 2 2 1 0
With multiple months inserted which is incorrect:
28/Feb 31/Mar 30/Apr 31/May 30/Jun 31/Jul 31/Aug 30/Sep 31/Oct 30/Nov 31/Dec 31/Jan
Exclude Company Name Tracking period (Days) Month 1 Month 2 Month 3 Month 4 Month 5 Month 6 Month 7 Month 8 Month 9 Month 10 Month 11 Month 12
10/22,11/22 CLH 90 0 0 0 0 0 0 0 2 3 8 6 5
Expected if multiple months as it has found one match for September so counts it
28/Feb 31/Mar 30/Apr 31/May 30/Jun 31/Jul 31/Aug 30/Sep 31/Oct 30/Nov 31/Dec 31/Jan
Exclude Company Name Tracking period (Days) Month 1 Month 2 Month 3 Month 4 Month 5 Month 6 Month 7 Month 8 Month 9 Month 10 Month 11 Month 12
10/22,11/22 CLH 90 0 0 0 0 0 0 0 1 1 1 0 0
Had to use MATCH with the TEXTSPLIT for it to work
=IF([#[Company Name]]="","",SUM(IF(ISNUMBER(SEARCH([#[Company Name]],RawData[Description]))=TRUE,IF(RawData[Home]=XLOOKUP($D$1,HomeList[Home Code],HomeList[Home]),IF(RawData[Source]="Spend Money",IF(RawData[Date]<=S$4,IF(RawData[Date]>=S$4-[#[Tracking period (Days)]],IF(ISNUMBER(MATCH(RawData[Find Date],TEXTSPLIT([#Exclude],","),)),0,1))))))))

Merge cells based on week number openpyxl

I am generating an excel sheet based on date range.
Along with date I am also finding week number.
Currently I am able to generate excel sheet like this below:
Date
Week
1/4/2021
1
1/5/2021
1
1/6/2021
1
1/7/2021
1
1/8/2021
1
1/9/2021
1
1/10/2021
1
1/11/2021
2
1/12/2021
2
1/13/2021
2
1/14/2021
2
1/15/2021
2
1/16/2021
2
1/17/2021
2
1/18/2021
3
1/19/2021
3
1/20/2021
3
1/21/2021
3
1/22/2021
3
1/23/2021
3
1/24/2021
3
I am expecting it like this:
Date
Week
1/4/2021
1
1/5/2021
1/6/2021
1/7/2021
1/8/2021
1/9/2021
1/10/2021
1/11/2021
2
1/12/2021
1/13/2021
1/14/2021
1/15/2021
1/16/2021
1/17/2021
1/18/2021
3
1/19/2021
1/20/2021
1/21/2021
1/22/2021
1/23/2021
1/24/2021
Used markdown tables here, But column showing week, those cells should be merged based on same week number.
code used:
from datetime import datetime
start_date = datetime.strptime("2021-01-01", "%Y-%m-%d")
end_date = datetime.strptime("2022-01-01", "%Y-%m-%d")
delta = timedelta(days=1)
wb = Workbook()
ws2 = wb.create_sheet("Test sheet")
row_number = 4
while start_date <= end_date:
ws2[f'A{row_number}'] = f"{start_date.month}/{start_date.day}/{start_date.year}"
ws2[f'B{row_number}'] = start_date.isocalendar()[1]
ws2[f'C{row_number}'] = start_date.strftime("%B")
row_number+=1
start_date += delta
You can change:
ws2[f'B{row_number}'] = start_date.isocalendar()[1]
to:
ws2[f'B{row_number}'] = start_date.isocalendar()[1] if start_date.weekday() == 0 else ''

Excel - Count based on criteria in 3 other columns

I'm looking for help in getting a count based on criteria in 3 other columns. I started to do a pivot table, but I cannot see how to add an IF statement to the distinct count there.
I need a count of each customer within the customer type, by each supplier, if the Cases > 0 for that year.
Here's a sample data set:
Supplier
Customer
Type
2019 Cases
2020 Cases
ABC
Al's Store
Package
3
2
ABC
Ben's
Package
0
6
ABC
Kroger
Grocery
2
1
ABC
Publix
Grocery
1
0
XYZ
Al's Store
Package
0
5
XYZ
Ben's
Package
4
0
XYZ
Kroger
Grocery
0
1
XYZ
Publix
Grocery
3
7
I need a result like this. My actual report will have each supplier on their own tab.
Supplier
Type
2019 Customer Count
2020 Customer Count
My Reason
ABC
Package
1
2
Al's bought in both years, but Ben's only in 2020
ABC
Grocery
2
1
Kroger bought in both years, but Publix only in 2019
XYZ
Package
1
1
Al only bought in 2020, Ben only bought in 2019
XYZ
Grocery
1
2
Kroger only bought in 2020
Thanks!

Calculate total number of open item at each month in Powerpivot

I want to find a way to calculate the total amount of open items in each month in powerpivot. My data looks like this...
Id Team CreatedDate ClosedDate
01 AAA 1/30/2014 5/13/2014
02 BBB 2/9/2014 2/18/2014
03 CCC 5/10/2014 9/15/2014
Result should look like this...
Month Count
Jan2014 1
Feb2014 2
Mar2014 1
Apr2014 1
May2014 2
Jun2014 1
Jul2014 1
Aug2014 1
Sep2014 1
Is any way to do this?
TIA...

Excel pivot table

I have a homework assingment. But i didn't get it exactly what i have to do.
"create a PivotTable with the smallest sales per agent and per article. there may be no grand totals shown. the field names must be in the PivotTable."
i did something like that. but it didn't seems like correct.
this is the table!
Row Labels Min of Sales
1-
1 12589
2 14700
2-
1 12300
2 12365
Date Artikel agent Channel Sales
3.01.2015 1 1 1 45612
3.01.2015 2 2 1 12365
4.01.2015 1 2 1 12345
4.01.2015 1 2 2 45230
5.01.2015 2 1 2 45610
6.01.2015 2 1 1 74102
6.01.2015 1 2 1 12300
7.01.2015 1 1 2 12589
7.01.2015 2 2 1 45600
9.01.2015 2 1 2 45600
9.01.2015 1 2 2 65400
10.01.2015 2 1 2 54600
10.01.2015 1 2 1 56400
13.01.2015 2 1 2 74100
13.01.2015 1 2 2 14700
14.01.2015 1 1 2 32500
14.01.2015 1 2 1 65200
18.01.2015 2 2 2 36900
18.01.2015 2 1 1 25800
22.01.2015 2 1 1 14700
22.01.2015 1 1 2 41700
23.01.2015 1 2 2 52800
23.01.2015 1 1 1 63900
26.01.2015 1 2 2 35700
26.01.2015 2 2 1 15900
27.01.2015 1 1 1 97100
28.01.2015 1 2 1 31700
31.01.2015 1 1 1 93100
If you Dont Want Grand Total, you can right Click on Row Labels and select Pivot Table Option and Select Totals & Filters And Uncheck the Show Grand Totals for Column.
Filed Names you can Manually Edit or You Can Choose Custom Name by Selecting Value Field Settings.
It sounds like you need to use the Agent and Artikel as the row labels, and remove the date.

Resources