Group by day from a certain hour of the day - sql

SQLServer 2008r2. I have a table which is populated with a record at 10 mins past every hour of every day. Every hour the job is run it also enters 48 records which represent a forecast of what is likely to happen for the next 48 hours. Note - Just before it enters the 48 hour forecast it deletes the forecast which was entered last time. So although it enters a 48 hour forecast every hour there is only ever one forecast in the system. The relevant fields in the table look like this:
currentScore obsDate
9 2017-06-22 08:10:00
9 2017-06-22 07:10:00
9 2017-06-22 06:10:00
10 2017-06-22 05:10:00
... ...
How can I query this table and group by day from a certain time of day? I would like the day to start at 6am the day before and finish at 6am on the day. I only need five records from the table, the day, two before and two in the future. So if its Jun 20 I want June 18, 19, 20, 21 and 22. Here is the query which gets the correct results by calendar day.
SELECT cast(obsDate AS DATE) AS theDate
,sum(CASE
WHEN currentScore < 8
THEN 1
ELSE 0
END) AS currentscore_low
,sum(CASE
WHEN currentScore >= 8
AND currentScore < 17
THEN 1
ELSE 0
END) AS currentscore_medium
,sum(CASE
WHEN currentScore >= 17
THEN 1
ELSE 0
END) AS currentscore_high
FROM diseaseScores
WHERE siteID = 8315
AND obsDate >= cast(getdate() - 2 AS DATE)
GROUP BY cast(obsDate AS DATE)
ORDER BY cast(obsDate AS DATE);
which returns this result:
theDAte low med high
2017-06-18 23 0 0
2017-06-19 22 0 0
2017-06-20 5 19 0
2017-06-21 0 24 0
2017-06-22 0 9 0
There is a new requirement to get the same result but the group by and the subsequent counts need to be from 6am to 6am. e.g
the first rec should be from 2017-06-17 06:00am to 2017-06-18 06:00am
the second rec should be from 2017-06-18 06:00am to 2017-06-19 06:00am
....etc
How can I do this? Thanks in advance
UPDATE, I have done two things:
1..introduce Tims idea
2..I also add an extra field 'numOfScores' to show how many hours worth of data
each line represent
select
cast(dateadd(hour, -6, obsDate) as date) as theDate, count(currentScore) as numOfScores,
sum(case when currentScore < 8 then 1 else 0 end) as currentscore_low,
sum(case when currentScore >= 8 and currentScore < 17
then 1 else 0 end) as currentscore_medium,
sum(case when currentScore >= 17 then 1 else 0 end) as currentscore_high
from diseaseScores
where siteID = 8315 and
obsDate >= cast(getdate() - 2 as date)
group by cast(dateadd(hour, -6, obsDate) as date)
order by cast(dateadd(hour, -6, obsDate) as date);
I now get this result:
2017-06-18 5 5 0 0
2017-06-19 24 23 1 0
2017-06-20 24 1 23 0
2017-06-21 24 8 16 0
2017-06-22 24 1 23 0
2017-06-23 9 0 9 0
This tells me that that there is only 5 hours worth of scores on the 2017-06-18. I want this first line to be 24 hours worth. From 6am on the 17th until 6am on the 18th. This makes me think I am not getting the result I wish
The 23rd only having 9 hours is ok because this is the most recent forecast
UPDATED:
I dont think its easily done in one query (if even possible) so I will just use five queries and specifically state the dates&times to get my outcome. e.g here are the first two:
select
sum(case when currentScore < 9 then 1 else 0 end) as numOfLOWRecs,
sum(case when currentScore > 8 and currentScore < 17 then 1 else 0 end) as currentscore_medium,
sum(case when currentScore >= 17 then 1 else 0 end) as currentscore_high
from diseaseScores where siteID = 9999
and obsDate >= '2017-06-18 06:00' and obsDate < '2017-06-19 06:00'
select
sum(case when currentScore < 9 then 1 else 0 end) as numOfLOWRecs,
sum(case when currentScore > 8 and currentScore < 17 then 1 else 0 end) as currentscore_medium,
sum(case when currentScore >= 17 then 1 else 0 end) as currentscore_high
from diseaseScores where siteID = 9999
and obsDate >= '2017-06-19 06:00' and obsDate < '2017-06-20 06:00'

One trick which might work here would be to simply shift each observation backwards by 6 hours. This would shift 2017-06-17 06:00:00 to 2017-06-17 00:00:00, i.e. now 6am becomes the start of that actual day.
select
cast(dateadd(hour, -6, obsDate) as date) as theDate,
sum(case when currentScore < 8 then 1 else 0 end) as currentscore_low,
sum(case when currentScore >= 8 and currentScore < 17
then 1 else 0 end) as currentscore_medium,
sum(case when currentScore >= 17 then 1 else 0 end) as currentscore_high
from diseaseScores
where siteID = 8315 and
obsDate >= cast(getdate() - 2 as date)
group by cast(dateadd(hour, -6, obsDate) as date)
order by cast(dateadd(hour, -6, obsDate) as date);

Related

T-SQL pivot inventory aging by day range

Writing a T-SQL statement to display items in inventory broken out by day range (pivot).
For example from this inventory table:
ItemName
DateCreated
PO_ID
A
2020-10-07
0
B
2020-10-07
1
A
2020-10-22
2
A
2020-10-22
2
A
2020-10-22
2
B
2020-10-29
3
Would like to generate the bellow results (typically a pivot table), showing the number of pieces per ItemName per day range. The date used to calculate the # of days since DateCreated would be the day the report was ran or passed in as a parameter - in the example shown here, the date used is from '2020-11-07':
ItemName
0-10 days
11-20 days
21-30 days
>30 days
A
0
3
0
1
B
1
0
0
1
Not sure what would be the best way to write the statement to generate the above results?
I would use conditional aggregation:
select itemName,
sum(case when datediff(day, dateCreated, getdate()) <= 10 then 1 else 0 end) as days_0_10,
sum(case when datediff(day, dateCreated, getdate()) > 10 and
datediff(day, dateCreated, getdate()) <= 20
then 1 else 0 end) as days_11_20,
sum(case when datediff(day, dateCreated, getdate()) > 20 and
datediff(day, dateCreated, getdate()) <= 30
then 1 else 0 end) as days_21_30,
sum(case when datediff(day, dateCreated, getdate()) > 30 then 1 else 0 end) as days_31
from t
group by itemName
I would use something similar to the following query SQL Server:
SELECT *
FROM
(
SELECT [ItemName],[PO_ID],
CASE
WHEN DATEDIFF(day, DateCreated, getdate()) BETWEEN 0 AND 10 THEN '0-10 days'
WHEN DATEDIFF(day, DateCreated, getdate()) BETWEEN 11 AND 20 THEN '11-20 days'
WHEN DATEDIFF(day, DateCreated, getdate()) BETWEEN 21 AND 30 THEN '21-30 days'
ELSE '>30 days'
END AS PeriodCreated
FROM [TableName])
) src
pivot
(
COUNT(PO_ID)
FOR PeriodCreated in ([0-10 days], [11-20 days], [>30 days])
) piv

Sum of particular column with month and year with fiscal year from custom date

I have following data in my table:
uniqueId d_date amount
1 2018-02-01 100.25
2 2019-03-01 456.5
3 2018-02-01 455
4 2019-05-01 200.48
5 2018-06-01 100
6 2019-07-01 200
7 2018-12-01 6950
8 2019-02-01 60
9 2020-01-20 100
Now when I enter start date = '2018-03-12' then my fiscal year must start with march 2018 to feb 2019 and so on.
If i enter start date = '2019-05-12' then my fiscal year must start with May 2019 to April 2020
I have tried below query but it is not working properly and also it calculate past year which is 2017 I do not want any data from past year from my entered custom date. So if entered start date = '2018-03-12' then is must start calculation for 2018 and above years only. No past year.
Declare #startdate as date
Declare #monthDate as int
Declare #ownmonth as int
set #startdate = '2018-03-12'
set #monthDate = month(#startdate)
set #ownmonth = 1
select
year(dateadd(month, -#monthDate, d_date)) year,
sum(case when month(d_date) = case when #monthDate+1 > 12 then #ownmonth else #monthDate+1 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+2 > 12 then #ownmonth+1 else #monthDate+2 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+3 > 12 then #ownmonth+2 else #monthDate+3 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+4 > 12 then #ownmonth+3 else #monthDate+4 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+5 > 12 then #ownmonth+4 else #monthDate+5 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+6 > 12 then #ownmonth+5 else #monthDate+6 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+7 > 12 then #ownmonth+6 else #monthDate+7 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+8 > 12 then #ownmonth+7 else #monthDate+8 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+9 > 12 then #ownmonth+8 else #monthDate+9 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+10 > 12 then #ownmonth+9 else #monthDate+10 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+11 > 12 then #ownmonth+10 else #monthDate+11 End then amount end) ,
sum(case when month(d_date) = case when #monthDate+12 > 12 then #ownmonth+11 else #monthDate+12 End then amount end) ,
sum(amount) total
from mytable
group by year(dateadd(month, -#monthDate, amount))
order by year
But above query does not show proper year & month wise data
Now I want output with fiscal year calculation:
Year Mar Apr May Jun July Aug Sept Oct Nov Dec Jan Feb Total
2018 - - - 100 - - - - - 6950 - 60 7110
2019 456.5 - 200.48 - 200 - - - - - 100 - 956.98
I can not use PIVOT as it is not supported in my compact SQL Server version.
How can I do this?
Your rule for a fiscal year is the year's March until the following year's February:
date | fiscal year
... | ...
2018-02-28 | 2017
2018-03-01 | 2018
... | 2018
2019-02-28 | 2018
2019-03-01 | 2019
... | ...
That means when we subtract two months from a date, we get a date the year of which is the fiscal year:
date | date - 2 months | fiscal year
... | ... | ...
2018-02-28 | 2017-12-28 | 2017
2018-03-01 | 2018-01-01 | 2018
... | ... | 2018
2019-02-28 | 2018-12-28 | 2018
2019-03-01 | 2019-01-01 | 2019
... | ... | ...
select
year(dateadd(month, -2, d_date)) as fiscal_year,
sum(case when month(d_date) = 3 then amount else 0 end) as mar,
sum(case when month(d_date) = 4 then amount else 0 end) as apr,
sum(case when month(d_date) = 5 then amount else 0 end) as may,
sum(case when month(d_date) = 6 then amount else 0 end) as jun,
sum(case when month(d_date) = 7 then amount else 0 end) as jul,
sum(case when month(d_date) = 8 then amount else 0 end) as aug,
sum(case when month(d_date) = 9 then amount else 0 end) as sep,
sum(case when month(d_date) = 10 then amount else 0 end) as oct,
sum(case when month(d_date) = 11 then amount else 0 end) as nov,
sum(case when month(d_date) = 12 then amount else 0 end) as dec,
sum(case when month(d_date) = 1 then amount else 0 end) as jan,
sum(case when month(d_date) = 2 then amount else 0 end) as feb,
sum(amount) as total
from mytable
group by year(dateadd(month, -2, d_date))
order by year(dateadd(month, -2, d_date));
If you want to limit this to the fiscal year a given date resides in, add:
where year(dateadd(month, -2, d_date)) = year(dateadd(month, -2, #given_date))
And well, if you want to limit this to the fiscal years beginning with that year, that would of course be:
where year(dateadd(month, -2, d_date)) >= year(dateadd(month, -2, #given_date))
UPDATE: You want a fiscal year to start with the first day of the month of a given date. I.e. If the given date is 1990-04-23, then a fiscal year starts with April. This changes above query only slightly, because rather than subtracting 2 months (for March), we must generalize this to subtracting one month less than the given month.
I am using a modulo operation when comparing months in order not to end up with months 13, 14, etc.
select
year(dateadd(month, - month(#startdate) + 1, d_date)) as fiscal_year,
sum(case when month(d_date) = (month(#startdate) + 0) % 12 then amount else 0 end) as first,
sum(case when month(d_date) = (month(#startdate) + 1) % 12 then amount else 0 end) as second,
sum(case when month(d_date) = (month(#startdate) + 2) % 12 then amount else 0 end) as third,
sum(case when month(d_date) = (month(#startdate) + 3) % 12 then amount else 0 end) as fourth,
sum(case when month(d_date) = (month(#startdate) + 4) % 12 then amount else 0 end) as fith,
sum(case when month(d_date) = (month(#startdate) + 5) % 12 then amount else 0 end) as sixth,
sum(case when month(d_date) = (month(#startdate) + 6) % 12 then amount else 0 end) as seventh,
sum(case when month(d_date) = (month(#startdate) + 7) % 12 then amount else 0 end) as eighth,
sum(case when month(d_date) = (month(#startdate) + 8) % 12 then amount else 0 end) as nineth,
sum(case when month(d_date) = (month(#startdate) + 9) % 12 then amount else 0 end) as tenth,
sum(case when month(d_date) = (month(#startdate) + 10) % 12 then amount else 0 end) as eleventh,
sum(case when month(d_date) = (month(#startdate) + 11) % 12 then amount else 0 end) as twelfth,
sum(amount) as total
from mytable
group by year(dateadd(month, - month(#startdate) + 1, d_date))
order by year(dateadd(month, - month(#startdate) + 1, d_date));
And again, if we want our results start from the fiscal year of the given date, we'd add:
where year(dateadd(month, - month(#startdate) + 1, d_date)) >= year(#startdate)

Sum Based on Date

I currently have this code that I want to sum every quantity based on the year. I have written a code that I thought would sum all the charges in 2016 and 2017, but it isn't running correctly.
I added the two different types of partition by statements to test and see if either would work and they don't. When I take them out, the Annual column just shows me the quantity for that specific receipt.
Here is my current code:
SELECT
ReceiptNumber
,Quantity
,Date
,sum(CASE WHEN (Date >= '2016-01-01' and Date < '2017-01-01') THEN
Quantity
ELSE 0 END)
OVER (PARTITION BY Date)
as Annual2016
,sum(CASE WHEN (Date >= '2017-01-01' and Date < '2018-01-01') THEN
Quantity
ELSE 0 END)
OVER (PARTITION BY ReceiptNumber)
as Annual2017
FROM Table1
GROUP BY ReceiptNumber, Quantity, Date
I would like my data to look like this
ReceiptNumber Quantity Date Annual2016 Annual2017
1 5 2016-01-05 17 13
2 11 2017-04-03 17 13
3 12 2016-11-11 17 13
4 2 2017-09-09 17 13
Here is a sample of some of the data I am pulling from:
ReceiptNumber Quantity Date
1 5 2016-01-05
2 11 2017-04-03
3 12 2016-11-11
4 2 2017-09-09
5 96 2015-07-08
6 15 2016-12-12
7 24 2016-04-19
8 31 2017-01-02
9 10 2017-0404
10 18 2015-10-10
11 56 2017-06-02
Try something like this
Select
..
sum(CASE WHEN (Date >= '2016-01-01' and Date < '2017-01-01') THEN
Quantity
ELSE 0 END)
OVER () as Annual2016
sum(CASE WHEN (Date >= '2017-01-01' and Date < '2018-01-01') THEN
Quantity
ELSE 0 END)
OVER ()as Annual2017
..
Where Date >= '2016-01-01' and Date < '2018-01-01'
If you want it printed only once at the top then you should run it in a separate query like:
SELECT YEAR(Date) y, sum(Quantity) s FROM Table1 GROUP BY YEAR(Date)
and then do the main query like this:
SELECT * FROM table1
Easy, peasey ... ;-)
Your original question could also be answered with:
SELECT *,
(SELECT SUM(Quantity) FROM Table1 WHERE YEAR(Date)=2016 ) Annual2016,
(SELECT SUM(Quantity) FROM Table1 WHERE YEAR(Date)=2017 ) Annual2017
FROM table1
You need some conditional aggreation over a Window Aggregate. Simply remove both PARTITION BY as you're already filtering the year in the CASE:
SELECT
ReceiptNumber
,Quantity
,Date
,sum(CASE WHEN (Date >= '2016-01-01' and Date < '2017-01-01') THEN
Quantity
ELSE 0 END)
OVER () as Annual2016
,sum(CASE WHEN (Date >= '2017-01-01' and Date < '2018-01-01') THEN
Quantity
ELSE 0 END)
OVER () as Annual2017
FROM Table1
You probably don't need the final GROUP BY ReceiptNumber, Quantity, Date

Group by day and conditional count issue

Using SQLServer 2008r2 - I have a table which has records inserted every hour. The relevant columns for my query are currentScore (int) and obsDate (smallDateTime). I wish to get five records grouped by day. Today, the two days prior to today (starting at mid-night) and two days in the future. SO if its Jun 20 I want June 18, 19, 20, 21 and 22. I am successfully doing this like so:
select dateadd(DAY,0, datediff(day,0, obsDate)) as theDate,
count(currentScore) as numOfScores
from diseaseScores
where siteID=8315 and obsDate > dateAdd(day, -2, (SELECT CONVERT(DATETIME,
CONVERT(DATE, CURRENT_TIMESTAMP)) + '00:00'))
group by dateadd(DAY,0, datediff(day,0, obsDate))
order by dateadd(DAY,0, datediff(day,0, obsDate))
My record set looks like so:
theDate numOfScores
2017-06-18 00:00:00.000 23
2017-06-19 00:00:00.000 22
2017-06-20 00:00:00.000 24
2017-06-21 00:00:00.000 24
2017-06-22 00:00:00.000 9
I wish to add three more columns which will count the number of currentScore in a certain range. Something like this
CASE
WHEN currentScore < 8 THEN COUNT(where currentScore < 8) as Low
WHEN currentScore > 8 and < 17 THEN COUNT(where currentScore > 8 and < 17) as Med
WHEN currentScore > 17 THEN COUNT(where currentScore > 17 ) as High
Can I do this with a select case? What is the best way to achieve this?
Thanks in advance
Here is the result I wish to achieve:
theDAte numOfScores low med high
2017-06-18 23 23 0 0
2017-06-19 22 22 0 0
2017-06-20 24 5 19 0
2017-06-21 24 0 24 0
2017-06-22 9 0 9 0
First, use cast(. . as date). Much clearer! Then you can do what you want using conditional aggregation:
select cast(obsDate as date) as theDate,
count(currentScore) as numOfScores ,
sum(case when currentScore < 8 then 1 else 0 end) as currentscore_low,
sum(case when currentScore >= 8 and currentScore < 17 then 1 else 0 end) as currentscore_medium,
sum(case when currentScore >= 17 then 1 else 0 end) as currentscore_high
from diseaseScores
where siteID = 8315 and
obsDate >= cast(getdate() - 2 as date)
group by cast(obsDate as date)
order by cast(obsDate as date);
Note: Your original where clause has only half the date condition. I didn't add the other half, but it should be pretty obvious how to get no more than two days in the future.

Date Diff- TSQL # months in each year between dates

Problem: Time Span between two dates. I would like to know how many months are between each date. The trick is: the number of months in each year between the two dates.
For example:
Start date = 1/1/2014
End Date = 3/1/2016
The output:
Column 1: "2014" would have a value of 12
Column 2: "2015" would have a value of 12
Column 3: "2016" would have a value of 2
This would be for a list with many dates (with different years)
EDIT: You would indeed have to have 14 year columns for a date span between 2000-2014. However, it is unlikely that more than 5 columns would need to be added.
Current train of thought
declare #datediff as int
select
#datediff=(Datediff(MONTH,[begin date], [end date]))
from [DateRange]
select
case
when #datediff <= 12 then #datediff
when #datediff <= 24 then #datediff -12
when #datediff <= 36 then #datediff -24
when #datediff <= 48 then #datediff -36
else NULL
end
from [DateRange]
Any ideas on this one?
I am very new to SQL and was only able to get the total months between the two with the following code:
select
datediff(MONTH,[begin date], [end date])
from [tableofdates]
Use below Query, you need to use your table in place of mydates table in below example. I used for maximum 10 year difference (represented by columns Y1,Y2 ... Y10).
The outer Query group by is used transpose the data to match to your requirement where you wanted month difference in column...
Inner query Q3 will provide the same results in rows with no limit to date range (actually there is limit i.e 2048 years due to master table master..spt_values which I guess you will not reach).
select
Q3.begindt,
Q3.enddt,
Q3.Diff_in_Year,
sum(Case when Q3.Year_Counter = 0 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y1,
sum(Case when Q3.Year_Counter = 1 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y2,
sum(Case when Q3.Year_Counter = 2 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y3,
sum(Case when Q3.Year_Counter = 3 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y4,
sum(Case when Q3.Year_Counter = 4 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y5,
sum(Case when Q3.Year_Counter = 5 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y6,
sum(Case when Q3.Year_Counter = 6 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y7,
sum(Case when Q3.Year_Counter = 7 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y8,
sum(Case when Q3.Year_Counter = 8 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y9,
sum(Case when Q3.Year_Counter = 9 Then datediff(mm,Q3.y_start,Q3.y_end)+1 else 0 end) Y10
From
(select
Q1.begindt,
Q1.enddt,
Q1.years Diff_in_Year,
Q2.number as Year_Counter,
(Case when Q2.number = 0 then Q1.begindt else dateadd(yy, datediff(yy,0,dateadd(yy,q2.number,q1.begindt)),0)End) AS y_Start,
(case when ((Q1.years-1) = Q2.number) then Q1.enddt else DATEADD(yy, DATEDIFF(yy,0,dateadd(yy,q2.number+1,q1.begindt) + 1), -1) End) AS y_End,
Year(Q1.begindt)+Q2.number YearInYYYY
from
(select begindt,enddt,DATEDIFF(year,begindt,enddt)+1 as years from mydates) Q1
join master..spt_values Q2 on Q2.type = 'P' and Q2.number < Q1.years
) Q3
Group by Q3.begindt,Q3.enddt,q3.Diff_in_Year
Output of the Above Query
begindt enddt YDif Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 Y9 Y10
2010-07-02 2014-02-06 5 6 12 12 12 2 0 0 0 0 0
2011-01-01 2014-12-31 4 12 12 12 12 0 0 0 0 0 0
2012-05-22 2017-12-16 6 8 12 12 12 12 12 0 0 0 0