February 29 comparing to February 28 of previous year - sql

I have a stored procedure that pulls data and joins last year's data on the Date. The problem is that the current year data has nothing to join on because there was no February 29th in 2011. Has anyone else experienced this problem? Anyone have any ideas on how to work around it?
Here is the stored procedure:
SELECT
--b.Date_Rep AS Date_Rep,
SUM(b.AccountsCreatedThisYear) AS AccountsCreatedThisYearTot,
SUM(a.AccountsCreatedThisYear) AS AccountsCreatedLastYearTot,
FROM Report2011.dbo.T_Report_01 b WITH (NOLOCK) --This year
LEFT JOIN Report2011.dbo.T_Report_01 a WITH (NOLOCK) ON DATEADD(yyyy,-1,b.Date_Rep) = a.date_rep --Last year
WHERE (a.Date_Rep BETWEEN DATEADD(year, -1,#StartDate) AND DATEADD(year, -1,#EndDate))

For starters, I wouldn't do a where clause on columns from a table on the outer side of a left join. Try this, instead:
SELECT SUM(b.AccountsCreatedThisYear) AS AccountsCreatedThisYearTot,
SUM(a.AccountsCreatedThisYear) AS AccountsCreatedLastYearTot,
FROM Report2011.dbo.T_Report_01 b WITH (NOLOCK) --This year
LEFT JOIN Report2011.dbo.T_Report_01 a WITH (NOLOCK)
ON DATEADD(yyyy,-1,b.Date_Rep) = a.date_rep --Last year
WHERE b.Date_Rep BETWEEN #StartDate AND #EndDate

Try using a FULL OUTER JOIN instead of a LEFT JOIN and use COALESCE:
SUM(COALESCE(b.AccountsCreatedThisYear, 0)) AS AccountsCreatedThisYearTot and
SUM(COALESCE(a.AccountsCreatedThisYear, 0)) AS AccountsCreatedLastYearTot
so you avoid the NULL's when the dates don't match.

declare #29Feb datetime = convert(datetime,'2012/02/29')
declare #28Feb datetime = convert(datetime,'2012/02/28')
select case when
dateadd(yy,-1,#29Feb) = dateadd(yy,-1,#28Feb)
then 1
else 0 end
This select statement outputs 1, so actually the Feb 29 and Feb 28 dates have only one corresponding date in the past year, Feb 28.
Now you are doing a sum for two periods in different years for which it happens that the first period has 1 day less than the current period.
How would someone answer to the following question:
"How many accounts have been created in the last year's February and how many this year?"
Does it matter the fact one February had 28 days and the other 29? I don't believe so, the reference is February, not the days.
So I see two problems with this query:
There might be a day this year when no accounts have been created, but in the last year, in the same period, some accounts did, so the left join doesn't catch the ones in the last year
For this year for two different dates, 28 and 29 correspond only one day, 28 so this one is summed twice.
SELECT (SUM(b.AccountsCreatedThisYear)
FROM Report2011.dbo.T_Report_01 WITH (NOLOCK) --This year
WHERE Date_Rep BETWEEN #StartDate and #EndDate ) as AccountsCreatedThisYearTot,
(SUM(b.AccountsCreatedThisYear)
FROM Report2011.dbo.T_Report_01 WITH (NOLOCK) -- Last Year
WHERE Date_Rep BETWEEN DATEADD(year, -1,#StartDate) AND DATEADD(year, -1,#EndDate)) as AccountsCreatedLastYearTot

There was no Feb 29 last year but March 1 was 365 days ago and Feb 28 was 366 days ago.
SELECT SUM(b.AccountsCreatedThisYear) AS AccountsCreatedThisYearTot,
SUM(a.AccountsCreatedThisYear) AS AccountsCreatedLastYearTot,
FROM Report2011.dbo.T_Report_01 b WITH (NOLOCK) --This year
LEFT JOIN Report2011.dbo.T_Report_01 a WITH (NOLOCK)
ON DATEADD(dd,-365,b.Date_Rep) = a.date_rep --Last year
WHERE b.Date_Rep BETWEEN #StartDate AND #EndDate
+1 Mark Bannister as I used his syntax

Related

How do I include months that have no data?

I am trying to create a report that shows how many training records will expire within a chosen date range however when I run the report it excludes months that have no training records going out of date. I have tried various solutions I've seen posted but I haven't been able to get any of them to work in my case.
This is my query:
SELECT COUNT(ISNULL(TRAININGRECORDID, 0)) AS NUMBEROFRECORDS
,DEPARTMENTNUMBER
,DATENAME( Month, EXPIRY ) + '-' + DATENAME( Year, EXPIRY ) AS [MONTHYEAR]
FROM Training_Records TR
JOIN Departments TD ON TR.DEPARTMENTID = TD.DEPARTMENTID
WHERE TR.EXPIRY IS NOT NULL
AND TD.DEPARTMENTNUMBER IN (#DEPTNO)
AND TR.EXPIRY BETWEEN #StartDate AND #EndDate
GROUP BY TD.DEPARTMENTNUMBER, DATENAME(Year, TR.EXPIRY), DATENAME(Month, TR.EXPIRY)
ORDER BY TD.DEPARTMENTNUMBER, [MONTHYEAR]
An example of results from this query looks like this:
NUMBEROFRECORDS DEPARTMENTNUMBER MONTHYEAR
1 21 April-2023
4 23 June-2023
1 83 August-2023
I am displaying the results of this query in a matrix with MONTHYEAR as the columns. In the example above the report will display April, June and August 2023 but will skip over the months May, July 2023 because there are no records going out of date in those months but despite that I still want them displayed in my report/returned in my query.
I've tried various solutions I've found on here but none of them have worked for me. How would I go about including these months with no records going out of date?
You need to first get all of the months, and then outer join to them (not using BETWEEN). Here is an example that gets April, May, June, and July, and then shows how you would outer join that against your table.
DECLARE #StartDate date = '20220405',
#EndDate date = '20220708';
;WITH Months(TheMonth) AS
(
SELECT DATEFROMPARTS(YEAR(#StartDate), MONTH(#StartDate), 1)
UNION ALL
SELECT DATEADD(MONTH, 1, TheMonth)
FROM Months
WHERE TheMonth < DATEFROMPARTS(YEAR(#EndDate), MONTH(#EndDate), 1)
)
SELECT TheMonth -- , COALESCE(SUM({your table}.{column}),0)
FROM Months AS m
-- LEFT OUTER JOIN {your table}
-- ON {your table}.{date column} >= m.TheMonth
-- AND {your table}.{date column} < DATEADD(MONTH, 1, m.TheMonth);
Output:
TheMonth
2022-04-01
2022-05-01
2022-06-01
2022-07-01
Example db<>fiddle
If your range could last more than 100 months, you'll need to add:
OPTION (MAXRECURSION 0);

Get consecutive months and days difference from date range?

So let's say I have a table like this:
subscriber_id
package_id
package_start_date
package_end_date
package_price_per_day
1081
231
2014-01-13
2014-12-31
$3.
1084
231
2014-03-21
2014-06-05
$3
1086
235
2014-06-21
2014-09-09
$4
Now I want the result for top 3 packages based on total revenue for each month for year 2014.
Note: For example for package 231 Revenue should be calculated such as 18 days of Jan * $3 +
28 days of feb * $3 + .... and so on.
For the second row the calculation would be same as first row (9 days of March* $3 + 30 days of April *$3 ....)
On the result the package should group by according to month and show rank depending on total revenue.
Sample result:
Month
Package_id
Revenue
Rank
Jan
231.
69499
1.
Jan.
235.
34345.
2.
Jan.
238.
23455.
3.
Feb.
231.
89274
1.
I wrote a query to filter the dates so that I get the active subscriber throughout the year 2014 (since initially there were values from different years),which shows the first table in the question, but I am not sure how do I break the months and days afterwards.
select subscriber_id, package_id, package_start_date, package_end_date
from (
select subscriber_id, package_id
, case when year(package_start_date) < '2014' then package_start_date = '01-Jan-2014' else package_start_date end as package_start_date
, case when year(package_start_date) > '2014' then package_end_date = '31-Dec-2014' else package_start_date end as package_end_date
, price_per_day
from subscription
) a
where year(package_start_date) = '2014' and year(package_end_date) = '2014'
Please do not emphasize on syntax - I am just trying to understand the logical approach in SQL.
Suppose you have a table that is a list of unique dates in a column called d, and the table is called d
It is then relatively trivial to do
SELECT *
FROM t
INNER JOIN d on d.d >= t.package_start_date AND d.d < t.package_end_date
Assuming you class a start date of jan 1 and an end date of jan 2 as 1 day. If you class as two, use <=
This will cause your package rows to multiply into the number of days, so start and end days of jan 1 and jan 11 would mean that row repeats 10 times. The d.d date is different on every row and you can extract the month from d.d and then group on it to give you totals for each month per package
Suppose you've CTEd that query above as x, it's like
SELECT DATEPART(month, x.dd), --the d.d date
package_id,
SUM(revenue)
FROM x
GROUP BY DATEPART(month, x.dd), package_id
Because the rows from T are repeated by Cartesian explosion when joined to d, you can safely group them or aggregate them to get them back to single values per month per package. If you have packages that stay with you more than a year you should also group on datepart year, to avoid mixing up the months from packages that stay from eg jan 2020 to feb 2021(they stay for two jans and two febs)
Then all you need to do is add the ranking of the revenue in, which looks like it would go in at the first step with something like
RANK(DATEDIFF(DAY, start, end)*revenue) OVER(PARTITION BY package_id)
I think I understand it correctly that you rank packages on total revenue over the entire period rather than per month.. look up the difference between rank and dense rank too as you may want dense instead

SQL to use getdate() to declare a variable to run a comparison against in SAP B1

I have a SAP B1 installation and I have a user table consisting of WeekNo, DateFrom, DateTo ...
It lists all the week numbers for each year, so the first week of the year is 202001 (yyyyww) and is set DateFrom 01/01/2020 and DateTo 07/01/2020
I am using getdate() to fetch todays date and using that to get the current week number.
What I am unable to see past is how to set the current week number to run a comparison against.
I have agoods in table also which has a field and is auto populated with the week number when it is created.
I need to be able to run this query without the user needed to enter the date or week number and for it to return all the goods in records for the current week.
So, getdate() to fetch the current week.
Compare that returned week (as a string) to all the week numbers (as a string) against all the goods in records and return where it matches.
Here is my sql, which is very incomplete.
getdate() as 'Date',
T1.[ItemCode],
T1.[Quantity]
FROM
OPDN T0
INNER JOIN PDN1 T1 ON T0.[DocEntry] = T1.[DocEntry]
INNER JOIN [dbo].[#DATE] T2 ON T0.[U_Wk] = T2.[Code]
WHERE getdate() BETWEEN T2.U_From and T2.U_to
AND T1.[ItemCode] NOT Like '%%STAT%%'
To explain the tables
T1.[Quantity] <-- number of each
T0/T1.[DocEntry] <-- GR record ID
T0.[U_Wk] <-- week number on the GR
T2.[Code] <-- week number used for the getdate() clause
T2.U_From and T2.U_to <-- dates that denote the week start and end
my apologies if I have misunderstood your question but this should help:
get current week
select DATEPART(wk, GETDATE())
get the document week number
select datepart(week, DocDate) from ORDR
for example
declare #thisWeek as nvarchar = cast(DATEPART(wk, GETDATE()) as nvarchar)
select
docnum
from ORDR
where cast(datepart(week, DocDate) as nvarchar) = #thisWeek

Modification todate dimension in SQL Server

I need a suggestion around one of the columns that I'm creating in the Date dimension in SQL Server, basically rolling weeks..
I have a table dimDate in my datawarehouse.
I want to create a column in the dimdate table which will have week number in any year and each week should have 7 days.
For eg: In year 2015 there are 53 weeks but the 53rd week has only 5 days (because the week starts on Sunday in SQL Server I guess).
I want to include 2 more days from 2016 (1st and 2nd Jan in 2016) to complete the 53rd week with 7 days and also the the 1st week in 2016 should start on 3rd of Jan 2016, so on and so forth.
If there are any suggestions that will be great to start with.
Assuming that you already have weeks populated (but not extended into the next year), and making some assumptions about columns names
This query finds the last week in a year (which would almost always always be 53 but don't count on it:) and the date that it ends on
SELECT YearNo, MAX(Week) As Week, MAX(DateKey) As DateKey
FROM dimDate
GROUP BY YearNo
This query finds all weeks that are shorter than 7 days, and how many extra days are required to make them 7 days.
SELECT
YearNo,
Week,
7-COUNT(DISTINCT DateKey) As ExtraDaysRequired
FROM dimDate
GROUP BY YearNo, Week
HAVING COUNT(DISTINCT DateKey) < 7
This might always be the last week of the year but lets not make assumptions.
Lets combine these to find all final weeks that have less than 7 days, as well as add the number of days required:
SELECT
Under7Days.YearNo, Under7Days.Week, Under7Days.ExtraDaysRequired,
FinalWeeks.DateKey StartDate,
DATEADD(d,Under7Days.ExtraDaysRequired,FinalWeeks.DateKey) EndDate
FROM
(
SELECT YearNo, MAX(Week) As Week, MAX(DateKey) As DateKey
FROM dimDate
GROUP BY YearNo
) As FinalWeeks
INNER JOIN
(
SELECT YearNo, Week, 7-COUNT(DISTINCT DateKey) As ExtraDaysRequired
FROM dimDate
GROUP BY YearNo, Week
HAVING COUNT(DISTINCT DateKey) < 7
) As Under7Days
ON FinalWeeks.Week = Under7Days.Week
AND FinalWeeks.YearNo = Under7Days.YearNo
So we have a query that identifies the start date and end date and week number that it needs to be updated to. So now we run an update:
UPDATE TGT
SET Week = SRC.Week
FROM dimDate TGT
INNER JOIN
(
SELECT
Under7Days.YearNo, Under7Days.Week, Under7Days.ExtraDaysRequired,
FinalWeeks.DateKey StartDate,
DATEADD(d,Under7Days.ExtraDaysRequired,FinalWeeks.DateKey) EndDate
FROM
(
SELECT YearNo, MAX(Week) As Week, MAX(DateKey) As DateKey
FROM dimDate
GROUP BY YearNo
) As FinalWeeks
INNER JOIN
(
SELECT YearNo, Week, 7-COUNT(DISTINCT DateKey) As ExtraDaysRequired
FROM dimDate
GROUP BY YearNo, Week
HAVING COUNT(DISTINCT DateKey) < 7
) As Under7Days
ON FinalWeeks.Week = Under7Days.Week
AND FinalWeeks.YearNo = Under7Days.YearNo
) SRC
ON TGT.DateID BETWEEN SRC.StartDate AND SRC.EndDate
Looks complicated? There's half a dozen ways to write the same thing but this approach is step-by-step. You could probably write a windowing function to do the same thing but I leave that as an exercise for someone else.

Fiscal Year To-Date in Where Clause (T-SQL)

Company's Fiscal Year: July 1 - June 30
I have a query where I am trying to capture aggregate # of units and $ revenue by product and cost center for the fiscal year-to-date. It will run on the 1st of the month and look through the last day of the previous month. Fiscal year does not appear in the report - it is criteria.
Mix of pseudocode and SQL:
Where
If datepart(mm,getdate()) - 1 < 7
THEN
transaction_post_date BETWEEN 7/1/ previous year AND dateadd(day,-(day(getdate()),getdate())
Else
transaction_post_date BETWEEN 7/1/current year AND dateadd(day,-(day(getdate()),getdate())
Am I on the write track? How do I write the SQL for a specific date on a year that depends on SQL - 7/1/current year?
I am weak using variables and do not even know if I have access to create them on the SQL Server DB, which is rather locked down. Definitely can't create a function. (I'm a business analyst.)
UPDATE, Fiscal year goes forward, so July 1, 2010, is Fiscal Year 2011.
I think this works:
Year(dateadd(month,6,htx.tx_post_date)) = Year(DateAdd(Month, 5, GetDate()))
Feeback?
And now I've been asked to add Fiscal Year-To-Date fields for quantity and revenue to the following query which gave me totals for
Select
inv.ITEM_CODE
, inventory.ITEM_NAME
, cc.COST_CENTER_CODE
, tx.REV_CODE_ID
, tx.PRICE
, tx.ITEM_SALE_ID
, sum(tx.quantity)
, sum(tx.amount)
from
transactions tx
inner join inventory inv on inv.item_id = tx.item_id
left outer join cost_center cc on cc.cost_center_id = tx.cost_center_id
where
DATEPART(mm, tx.tx_date) = DATEPART(mm,dateadd(m,-1,getdate()))
and DATEPART(yyyy, tx.tx_date) = DATEPART(yyyy,dateadd(m,-1,getdate()))
group by
inv.ITEM_CODE
, inventory.ITEM_NAME
, cc.COST_CENTER_CODE
, tx.REV_CODE_ID
, tx.PRICE
, tx.ITEM_SALE_ID
I need to add the fiscal year-to-date quantity and and amount columns to this report. Would a correlated subquery by the way to go? Would the joins be tricky? I've never used a subquery with an aggregation/grouping query.
Thanks for all the previous help.
Here is how I would do it if I needed to group by Fiscal Year:
Group by Year(DateAdd(Month, -6, TransactionDate))
May be not exactly it, but you get the idea.
I would add a calculated column to your table called FiscalYear (with the proper calculation) and select based on that column
I believe the easiest way is to do this in two steps. Use the WHERE Clause to filter your YTD and then a GROUP BY to group by FY. Since your FY begins in July(7) then increment the FY if the month is greater than June(6).
WHERE CLAUSE:
WHERE
DATEDIFF(DAY, transaction_post_date, Cast(Month(GetDate()) as varchar) +
'/' + Cast(Day(GetDate()) as varchar) + '/' + CAST(Case WHEN
MONTH(transaction_post_date) > 6 then YEAR(transaction_post_date) + 1 else
Year(transaction_post_date) end as varchar)) >=0
GROUP BY CLAUSE:
GROUP BY CASE WHEN MONTH(transaction_post_date) > 6 then
Year(transaction_post_date) + 1 else YEAR(transaction_post_date) end