RDLC How to Group by row with time range - rdlc

I am using linq to get the value and passing this to the reportviewer.
Let say for a particular Date from Sept 29 8Am to Sept 30 8am.
I would say there is a shift from 8am of (date) to 8am of(next date)
How can i achieve a report like this?
Date Name Value
Sept 29,2014 8:00 Am mango 10
Sept 30,2014 8:00 Am apple 20
Sum 30
Sept 30,2014 9:00 Am mango 30
Oct 01,2014 1:00 Am apple 20
Sum 50
Thanks

To do any sort of grouping, you will need to have something in common between the records to do that grouping on. It looks like you are trying to group data based on a 24 hour period starting at 8am on one day and going to 8am the next day.
One problem with this approach is that the end of the grouping includes the start of the next in terms of the date. So do you want to group the data from 8:00:00am to 7:59:59am the next day or 8:00:01am to 8:00:00am the next day.
Now once that decision has been done, then you could use a formula to create the grouping based on the data.
something like the following (concept only, will need adjusting to your situation)
Grouping Date = (data.DateTime.TimeComponent >= 8:00:00am) then data.Date else data.Date - 1 day
EDIT
so you would end up with as your data
Date Name Value GroupingDate
Sept 29,2014 8:00 Am mango 10 Sept 29, 2014
Sept 30,2014 8:00 Am apple 20 Sept 29, 2014
Sept 30,2014 9:00 Am mango 30 Sept 30, 2014
Oct 01,2014 1:00 Am apple 20 Sept 30, 2014

Related

Produce weekly and quarterly stats from a monthly figure

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?

Oracle results from last X days while ignoring time

Right now I have the following query which returns results from the last 60 days
select * from my_table where date_col > sysdate - 60
But it is also taking time of the day into consideration. For example today is
Sept 30 2021 10:30:00 AM
and the query would return results from Oct 01 2021 10:31:00 AM, but not from Oct 01 2021 10:29:00 AM
How can I modify the query that it does not care about the time when getting the last 60 days? I would the query to return results even if the row had a date of Oct 01 2021 00:00:01 AM
It sounds like you just want to trunc the sysdate. I'd guess that you want to do a >= as well.
WHERE date_col >= trunc(sysdate) - 60

How to get column value comparison in sql?

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];

Sum of Previous Yr

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

Generating report using IF THEN SQL query

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.