I am having issues getting a SQL statement to work how I need it to. To be honest, I'm pretty green when it comes to SQL so the tries I've attempted have come from copy/paste code that I've tried to edit to make work and it's not running. So what I need is a query to be used for a report in ACCESS.
Here is what data look like:
ID TechID OccurrenceDate OccurrenceName OccurrenceAmt
247 9991 Friday, February 15, 2013 Coaching 4.50
242 9991 Friday, February 08, 2013 Con't Occurrence 0.00
241 9991 Thursday, February 07, 2013 Unscheduled Absense 1.00
240 9991 Wednesday, February 06, 2013 Shift Int less 2 hrs 0.50
243 9991 Monday, February 04, 2013 Unscheduled Absense 1.00
246 9991 Monday, January 21, 2013 Unscheduled Absense 1.00
245 9991 Wednesday, January 16, 2013 Con't Occurrence 0.00
244 9991 Tuesday, January 15, 2013 Unscheduled Absense 1.00
239 9999 Friday, February 08, 2013 Unscheduled Absense 1.00
237 9999 Wednesday, February 06, 2013 Unscheduled Absense 1.00
238 9999 Saturday, February 02, 2013 Coaching 7.00
236 9999 Tuesday, September 11, 2012 Other 6.00
235 9999 Tuesday, September 11, 2012 Other 0.00
228 9999 Thursday, August 23, 2012 Unscheduled Absense 1.00
227 9999 Friday, August 10, 2012 Unscheduled Absense 1.00
226 9999 Wednesday, August 08, 2012 Con't Occurrence 0.00
223 9999 Wednesday, February 29, 2012 Unscheduled Absense 1.00
249 9998 Saturday, February 02, 2013 Unscheduled Absense 1.00
251 9998 Monday, January 21, 2013 Unscheduled Absense 1.00
So basically if there is an "OccurrenceName" of either "Coaching" or "Other" within the last 6 months that amount plus any other occurrences within the previous 6 months should be their Tech Total. If there is are no "Coaching" or "Other" occurrences within the last 6 months then I need to sum the OccurrenceAmount for just the rolling previous 6 months.
Hopefully my very well explained scenario makes sense.
EDIT #1:
Okay, my expected output for this data should be:
TechID Total
9991 4.5
9999 9.0
9998 2.0
So as you can see, for TechID 9991 calculates 4.5 because there was a "Coaching" occurrence and nothing since in the previous 6 months. 9999 would have 9 because there was a Coaching for 7 and two more since then within the previous 6 months bringing that total to 9. 9998 has 2 because that tech has no coaching or anything within the last 6 months so the total is 2.
EDIT #2:
So the only lines that should be counted are the lines that are indented. For 9999, there was a coaching for 7 and 2 more regular occurrences bringing his total to 9. Is that more clear?
EDIT #3:
Okay, got a little further down the road.
#lance - through trial and error I am getting closer... have this for now, but can't get it working:
SELECT tblEmployeeData.TechID, tblEmployeeData.LName, tblEmployeeData.FName, Sum(tblOccurrence.OccurrenceAmt), Last(tblOccurrence.CoachingDate) AS LastOfCoachingDate, tblEmployeeData.SupLName
FROM tblEmployeeData RIGHT JOIN tblOccurrence ON tblEmployeeData.TechID = tblOccurrence.TechID
GROUP BY tblEmployeeData.TechID, tblEmployeeData.LName, tblEmployeeData.FName, tblEmployeeData.SupLName
HAVING (((tblOccurrence.OccurrenceAmt))=IIf([tblOccurrence].[CoachingDate]="",[tblOccurrence].[OccurrenceDate] Between Date() And DateAdd('m',-6,Date()),IIf([tblOccurrence].[CoachingDate]<=DateAdd('m',-6,Date()),[tblOccurrence].[OccurrenceDate] Between Date() And DateAdd('d',[tblOccurrence].[CoachingDate],Date()))));
EDIT #4:
This query is the "best" beginning query I have gotten to work. It pulls over ALL employee data then populates MaxCoaching and MaxDate. So I tried connecting this query to your second query to get totals onto a query and can't get it working.
Query:
SELECT tblEmployeeData.TechID, tblEmployeeData.LName, tblEmployeeData.FName, Max(tblOccurrence.CoachingDate) AS LastCoachingDate, Max([OccurrenceDate]) AS MaxDate, tblEmployeeData.SupLName
FROM tblEmployeeData LEFT JOIN tblOccurrence ON tblEmployeeData.TechID = tblOccurrence.TechID
GROUP BY tblEmployeeData.SupLName, tblEmployeeData.TechID, tblEmployeeData.LName, tblEmployeeData.FName
So these results get the most recent Coaching Date (if any) and the most recent event date so I need to sum Occurrences based on 2 conditions:
If there is a coaching/other date within the last 6 months, it needs that occurrence total from that line PLUS any other dates that have occurred after their coaching/other date
If no coaching/other date has occurred within the last 6 months, then I need the total of occurrences within the last 6 months.
Moving closer to getting a working query! Thanks for your help
#Zamael if I understand your question and the clarifications, the reason 9998 sums to 9, is that the September dates are more than 6 months ago, and that there is another line item with an OccurenceAmt of 2 that we don't see in the table.
Assuming that's correct, I suggest that you create a query with the following SQL:
Select TechID,
SUM(IIF([OccurenceName] in ('Coaching','Other'),1,0)) as CoachingOtherCount,
SUM(IIF([OccurenceName] in ('Coaching','Other'),OccurenceAmt,0)) as CoachingAmt,
SUM(IIF(IIF([OccurenceName] in ('Coaching','Other'),0,OccurenceAmt))) as MiscAmt
From TableName
Where OccurenceDate > = DateAdd("m",-6,Date())
Group By TechID;
Then, Create a second query with following SQL:
select TechID,
IIF(Nz(CoachingOtherCount,0) > 0, CoachingAmt, MiscAmt) as SumOccurenceAmt
from Query1;
EDIT 1 - After Clarification
What I understand now is:
Step 1: Find "Coaching" or "Other" in past 6 Months. If exists goto 2, else goto 3.
Step 2: Sum All lines occurring on or after the earliest from Step 1.
Step 3: Sum all lines from past 6 months.
If this is what you're looking for then the following code will work.
SELECT TechID,
MIN(IIF([OccurrenceName] in ('Coaching','Other'),OccurrenceDate,NULL)) AS MinCoachingOtherDate,
MIN([OccurrenceDate]) AS MinDate
FROM tblOccurrence
WHERE OccurrenceDate >= DateAdd("m",-6,Date())
GROUP BY TechID;
Then, in the second query
Select tbo.TechID,
SUM([OccurrenceAmt]) as Amount
From tblOccurrence tbo
LEFT JOIN QUERY1 q on q.techid = tbo.techid
WHERE tbo.OccurrenceDate >= IIF(q.MinCoachingOtherDate IS NULL, q.MinDate, q.MinCoachingOtherDate)
GROUP BY tbo.TechID;
The first query finds the earliest date for Coaching/Other in the first column if it exists, and simply the earliest date in the second. Then, in the second query we assume the Coaching/Other date unless it is null, then we default to the second date. Because the where constrains the dates dynamically for each TechID, you can just sum all of the OccurenceAmt.
Related
I have a sample of a table as below:
Customer Ref
Bear Rate
Distance
Month
Revenue
ABA-IFNL-001
1000
01/01/2022
-135
ABA-IFNL-001
1000
01/02/2022
-135
ABA-IFNL-001
1000
01/03/2022
-135
ABA-IFNL-001
1000
01/04/2022
-135
ABA-IFNL-001
1000
01/05/2022
-135
ABA-IFNL-001
1000
01/06/2022
-135
I also have a sample of a calendar table as below:
Date
Year
Week
Quarter
WeekDay
Qtr Start
Qtr End
Week Day
04/11/2022
2022
45
4
Fri
30/09/2022
29/12/2022
1
05/11/2022
2022
45
4
Sat
30/09/2022
29/12/2022
2
06/11/2022
2022
45
4
Sun
30/09/2022
29/12/2022
3
07/11/2022
2022
45
4
Mon
30/09/2022
29/12/2022
4
08/11/2022
2022
45
4
Tue
30/09/2022
29/12/2022
5
09/11/2022
2022
45
4
Wed
30/09/2022
29/12/2022
6
10/11/2022
2022
45
4
Thu
30/09/2022
29/12/2022
7
11/11/2022
2022
46
4
Fri
30/09/2022
29/12/2022
1
12/11/2022
2022
46
4
Sat
30/09/2022
29/12/2022
2
13/11/2022
2022
46
4
Sun
30/09/2022
29/12/2022
3
14/11/2022
2022
46
4
Mon
30/09/2022
29/12/2022
4
15/11/2022
2022
46
4
Tue
30/09/2022
29/12/2022
5
16/11/2022
2022
46
4
Wed
30/09/2022
29/12/2022
6
17/11/2022
2022
46
4
Thu
30/09/2022
29/12/2022
7
How can I join/link the tables to report on revenue over weekly and quarterly periods using the calendar table? I can put into two tables if needed as an output eg:
Quarter Starting
31/12/2021
01/04/2022
01/07/2022
30/09/2022
Quarter
1
2
3
4
Revenue
500
400
540
540
Week Date Start
31/12/2021
07/01/2022
14/01/2022
21/01/2022
Week
41
42
43
44
Revenue
33.75
33.75
33.75
33.75
I am using alteryx for this but wouldnt mind explaination of possible logic in sql to apply it into the system
Thanks
Before I get into the answer, you're going to have an issue regarding data integrity. All the revenue data is aggregated at a monthly level, where your quarters start and end on someday within the month.
For example - Q4 starts September 30th (Friday) and ends Dec. 29th (Thursday). You may have a day or two that bleeds from another month into the quarters which might throw off the data a bit (esp. if there's a large amount of revenue during the days that bleed into a quarter.
Additionally, your revenue is aggregated at a monthly level - unless you have more granular data (weekly, daily would be best), it doesn't make sense to do a weekly calculation since you'll probably just be dividing revenue by 4.
That being said - You'll want to use a cross tab feature in alteryx to get the data how you want it. But before you do that, we want to aggregate your data at a quarterly level first.
You can do this with an if statement or some other data cleansing tool (sorry, been a while since I used alteryx). Something like:
# Pseudo code - this won't actually work!
# For determining quarter
if (month) between (30/09/2022,29/12/2022) then 4
where you can derive the logic from your calendar table. Then once you have the quarter, you can join in the Quarter Start date based on your quarter calculation.
Now you have a nice clean table that might look something like this:
Month
Revenue
Quarter
Quarter Start Date
01/01/2022
-135
4
30/09/2022
01/01/2022
-135
4
30/09/2022
Aggregate on your quarter to get a cleaner table
Quarter Start Date
Quarter
revenue
30/09/2022
4
300
Then use cross tab, where you pivot on the Quarter start date.
For SQL, you'd be pivoting the data. Essentially, taking the value from a row of data, and converting it into a column. It will look a bit janky because the data is so customized, but here's a good question that goes over pivioting - Simple way to transpose columns and rows in SQL?
I have a dataset of events each of which occurred on a specific day. Using Pandas I have been able to aggregate these into a count of events per month using the groupby function, and then plot a graph with Matplotlib. However, in the original dataset some months do not have any events and so there is no count of events in such a month. Such months do not therefore appear on the graph, but I would like to include then somehow with their zero count
bpm2 = df2_yr1.groupby(['month_year', 'month'])['event_no'].count()
which produces
month_year month
2016-01 January 9
2016-02 February 7
2016-04 April 1
2016-06 June 4
2016-07 July 1
2016-08 August 3
2016-09 September 2
2016-10 October 5
2016-11 November 17
2016-12 December 3
I have been trying to find a way of filling missing months in the dataframe generated by the groupby function with a 'count' value of 0 for, in this example, March and May..
Can anyone offer some advice on how this might be achieved. I have been trying to carry out FFill on the month column but with little success and can't work out how to add in a corresponding zero value for the missing months
First of all, if bpm2 = df2_yr1.groupby(['month_year', 'month'])['event_no'].count() is your code, then it is a series. So, let's change it to a dataframe with bpm2 = df2_yr1.groupby(['month_year', 'month'])['event_no'].count().reset_index(). Now, into the problem.
Change to date format and use pd.Grouper and change back to string format. Also add back the month column and change the formatting of the event_no column:
bpm2 = df2_yr1.groupby(['month_year', 'month'])['event_no'].count().reset_index()
bpm2['month_year'] = bpm2['month_year'].astype(str)
bpm2['month_year'] = pd.to_datetime(bpm2['month_year'])
bpm2 = bpm2.groupby([pd.Grouper(key='month_year', freq='1M')])['event_no'].first().fillna(0).astype(int).reset_index()
bpm2['month'] = bpm2['month_year'].dt.strftime('%B')
bpm2['month_year'] = bpm2['month_year'].dt.strftime('%Y-%m')
bpm2
output:
month_year event_no month
0 2016-01 9 January
1 2016-02 7 February
2 2016-03 0 March
3 2016-04 1 April
4 2016-05 0 May
5 2016-06 4 June
6 2016-07 1 July
7 2016-08 3 August
8 2016-09 2 September
9 2016-10 5 October
10 2016-11 17 November
11 2016-12 3 December
I have a table as below. The table holds the price of a product for each day in a year. I would like to get price change for each day by year.
Product Year 1Jan 2Jan .................... 31Dec
A 2018 10 20 .................... 120
A 2019 130 150 .................... 200
B 2018 15 23 .................... 90
B 2019 113 130 .................... 220
I would like to compare columns sequentially with year overlaps and get output as below.
• For the year 2018, by negating the value 2 Jan from 1 Jan (2 Jan-1 Jan), we get the new value of 2 Jan.
• For the year 2018, by negating the value 3Jan from 2 Jan (3 Jan-2 Jan), we get the new value of 3 Jan.
• For the year 2018, by negating the value 31Dec from 30 Dec (31 Dec-30 Dec), we get the new value of 31 Dec
• Now, For the year 2019, by negating the value 31 Dec(2018 year) from 1 Jan (2019 year), we get the new value of 1 Jan, 2019
So, in a nutshell, the value of a column is the difference of its value with previous day value.
Product Year 1Jan 2Jan .................... 31Dec
A 2018 10 10 .................... 15 (just assume value of 30Dec column is 105)
A 2019 10 20 .................... 10 (just assume value of 30Dec column is 190)
B 2018 15 8 .................... 8 (just assume value of 30Dec column is 82)
B 2019 23 17 .................... 10 (just assume value of 30Dec column is 210)
Let me know, if things are not clear.
Though logically there is nothing in this query, but still you have to work hard to write it -
SELECT Product
,Year
,1Jan
,2Jan - 1Jan 2Jan
,3Jan - 2Jan 3Jan
.
.
.
,31Dec - 30Dec 31Dec
FROM YOUR_TAB
ORDER BY Product
,Year;
first of all I think the design of the table could be better but thats a topic for some other time. Right now below code should work -
SELECT Product, Year,
1Jan AS '1st Jan',
2Jan-1Jan AS '2nd Jan',
3Jan-2Jan AS '3rd Jan',
4Jan-3Jan AS '4th Jan',
.
.
.
.
.
31Dec-30Dec AS '31st Dec',
FROM [table name];
I would like a column displaying the fiscal week. Our fiscal year begins in April.
So far I have the below, using datename(ww,DateAndTime) as Week
DateAndTime Week
2015-04-01 22:45 14
2015-06-14 13:22 25
2015-12-02 09:15 49
2016-01-01 07:35 1
I would like the output to show:
DateAndTime Week Fiscal Week
2015-04-01 22:45 14 1
2015-06-14 13:22 25 12
2015-12-02 09:15 49 36
2016-01-01 07:35 1 41
While I don't understand the logic behind the fiscal week (the difference between 1 and 41 is 40, but between 14 and 1 it's 39), maybe I'm missing something or you made a typo.
However, in general you'd do something like this (assuming the difference is 40 weeks):
SELECT week, (week+40)%52 AS fw FROM ...
If the fiscal year starts at a different week every each (say, 13th or 14th week depending on year), you can use the date and time functions, but they may vary between SQL versions. In MySQL you have YEAR(), MONTH(), WEEK(), etc.
For example:
SELECT week, (week+(52-WEEK(CONCAT_WS('-', YEAR(NOW()), '04-01'))))%52 FROM ...
But it might be overkill.
Note: It is possible to count the other way: if you subtract the diff from the week instead of adding, you will need to add 52 if the number is negative. You can do that by adding 52 and then doing modulo (%) 52.
I have a simple query which does the below:
SELECT
B.WEEK_DT WEEK_DT,
SUM(A.PROFIT) PROFIT
FROM
CUSTOMERS A
INNER JOIN WEEK_TABLE B
ON A.WEEK_ID = B.WEEK_ID
Now, I want to extend this query to get Sum of profit for all of yr 2013. That means, the above data gives me value at weekly level and i also want a separate column which give me 2013_Profit, summing up all weeks of previous yr.
week_dt is in the format of mm-dd-yyyy
also, we have an offset in the week table, if that helps:
- WK_OFFSET WK_DT
-13 February 22, 2014
-12 March 1, 2014
-11 March 8, 2014
-10 March 15, 2014
-9 March 22, 2014
-8 March 29, 2014
-7 April 5, 2014
-6 April 12, 2014
-5 April 19, 2014
-4 April 26, 2014
-3 May 3, 2014
-2 May 10, 2014
-1 May 17, 2014
Please let me know how i can get another column for each customer which gives a sum previous yr profits.
Some thing like the below:
Customer Curr_WK_Profit Prev_YR_Profit
AAA 10 520
BBB 20 1040
CCC 30 1560