March 15, 2016 at 12:14 pm
Here's what I'm trying to accomplish....I'm pulling week to date sales data (ex. if I were to run it tomorrow it would pull sales totals for Monday and Tuesday of this week). But, I want to be able to compare it to the sales of week to date (Monday and Tuesday of the same week last year). Is that possible?
March 15, 2016 at 12:29 pm
i think if you group your data by week or by day (with a filter for a weeks worth of data, and then join it against itself, you'll get what you are after.
'
i think if you add -q for quoted identifier, you get what you are after with the quote.s
here's a simple example
WITH MyData
AS
(
SELECT COUNT(*) AS TotalInvoices,
SUM(InvoiceAmount) AS SumInvoices,
DATEADD(wk, DATEDIFF(wk,0,InvoiceDate), 0) AS InvoiceWeek,
DATEADD(dd, DATEDIFF(dd,0,InvoiceDate), 0) AS InvoiceDay
FROM Invoices GROUP BY
DATEADD(wk, DATEDIFF(wk,0,InvoiceDate), 0),
DATEADD(dd, DATEDIFF(dd,0,InvoiceDate), 0)
)
SELECT
T1.InvoiceWeek,
T1.InvoiceDay,
T2.TotalInvoices AS PriorWkTotalInvoices,
T2.SumInvoices AS PriorWkSumInvoices,
T1.TotalInvoices AS CurrentWkTotalInvoices,
T1.SumInvoices AS CurrentWkSumInvoices
FROM MyData T1
LEFT JOIN T2
ON DATEADD(dd,-7, T1.InvoiceWeek) = T2.InvoiceWeek
AND DATEADD(dd,-7, T1.InvoiceDay) = T2.InvoiceDay;
--WHERE T1.InvoiceWeek = DATEADD(wk, DATEDIFF(wk,0,GETDATE()), 0) --this week
Lowell
March 15, 2016 at 12:30 pm
I'm pulling week to date sales data (ex. if I were to run it tomorrow it would pull sales totals for Monday and Tuesday of this week). But, I want to be able to compare it to the sales of week to date (Monday and Tuesday of the same week last year). Is that possible?
Cory, where are you doing your analysis? I'm asking because the answer will depend on where you're doing the analysis. If you did it from PowerPivot or PowerBI, I would say to build a quick data warehouse (Fact table, Date Dimension, etc). Then you could use PowerPivot and the PowerPivot functions like SAMEPERIODLASTYEAR. If you're doing it purely in T-SQL, the answer would be a little different.
March 15, 2016 at 12:36 pm
pietlinden, I'm building this via sql in sql server.
March 15, 2016 at 12:36 pm
Lowell, how does T2 know to look at last year's data for the same days?
March 15, 2016 at 12:53 pm
cory.bullard76 (3/15/2016)
Lowell, how does T2 know to look at last year's data for the same days?
Last YEARS data?
i thought you said last week.
the join criteria is the datediff info: i'm subtracting seven days so i can join it to last week.
you would change this to whatever period you wanted(year/month etc)
LEFT JOIN T2
ON DATEADD(dd,-7, T1.InvoiceWeek) = T2.InvoiceWeek
AND DATEADD(dd,-7, T1.InvoiceDay) = T2.InvoiceDay;
Lowell
March 15, 2016 at 1:05 pm
Removed the "compare to last week" part...
looks like there's a piece missing:
FROM MyData T1
LEFT JOIN T2
ON DATEADD(yyyy,-1,T1.InvoiceDate) = T2.InvoiceDate
(I think).
March 15, 2016 at 1:45 pm
Let's say you run this yesterday, which is Monday of week 12, which goes from March 13 to March 19.
Should you compare it to week 12 of 2015? Or week 11 of 2015 which includes March 14?
What happens when a year has week 53? How do you define weeks and week numbers?
All these are business related questions you should have asked (unless the answers were defined previously).
If you have a calendar table, it's easier to do this kind of query.
March 15, 2016 at 2:09 pm
You are on SQL Server 2012. What you ask seems tailor made for the windowing function enhancement that came with that version. LAG in this case I would think.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
March 16, 2016 at 7:32 am
Thanks everyone. I found a calendar that has fiscal year that I can work from.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply