Noreen McHugh
asked on
SQL 2008 week to date total
I have created a view from a table that shows daily sales versus and the comparable days sales from last year.When reporting this data I need to also show a week to date running total for every day on the Gross sale figure.
Our week runs from Monday to Sunday.
Is adding a column to the view which calculates the running total the best way to go about this or is doing the calculation at the reporting level a better plan??
Also I am unsure on how to actually calculate the running total.
I presume I will also have to work out the day of the week also to facilitate this??
Ani ideas or suggestions would be appreciated.
Thanks
Noreen
Our week runs from Monday to Sunday.
Is adding a column to the view which calculates the running total the best way to go about this or is doing the calculation at the reporting level a better plan??
Also I am unsure on how to actually calculate the running total.
I presume I will also have to work out the day of the week also to facilitate this??
Ani ideas or suggestions would be appreciated.
Thanks
Noreen
SELECT TOP (100) PERCENT dbo.[Gross Nett].Fiscaldate, dbo.[Gross Nett].RestaurantCode, dbo.[Gross Nett].Gross + ISNULL(dbo.AndroRefunds.Amount, 0) AS Gross,
dbo.[Gross Nett].Nett, dbo.[Gross Nett].Orders, dbo.Comps.FiscalDate AS Expr1, dbo.Comps.Comps, dbo.Comps.RestaurantCode AS Expr2,
dbo.Slices.FiscalDate AS Expr3, dbo.Slices.RestaurantCode AS Expr4, dbo.Slices.Slices, dbo.[Gross Nett].Fiscaldate - 364 AS LastYearDate,
dbo.AndroRefunds.Amount
FROM dbo.[Gross Nett] LEFT OUTER JOIN
dbo.AndroRefunds ON dbo.[Gross Nett].Fiscaldate = dbo.AndroRefunds.FiscalDate AND
dbo.[Gross Nett].RestaurantCode = dbo.AndroRefunds.RestaurantCode LEFT OUTER JOIN
dbo.Slices ON dbo.[Gross Nett].Fiscaldate = dbo.Slices.FiscalDate AND dbo.[Gross Nett].RestaurantCode = dbo.Slices.RestaurantCode LEFT OUTER JOIN
dbo.Comps ON dbo.[Gross Nett].Fiscaldate = dbo.Comps.FiscalDate AND dbo.[Gross Nett].RestaurantCode = dbo.Comps.RestaurantCode
ORDER BY dbo.[Gross Nett].Fiscaldate, dbo.[Gross Nett].RestaurantCode
SELECT TOP (100) PERCENT dbo.[Gross Nett].Fiscaldate, dbo.[Gross Nett].RestaurantCode, dbo.[Gross Nett].Gross + ISNULL(dbo.AndroRefunds.Amount, 0) AS Gross,
dbo.[Gross Nett].Nett, dbo.[Gross Nett].Orders, dbo.Comps.FiscalDate AS Expr1, dbo.Comps.Comps, dbo.Comps.RestaurantCode AS Expr2,
dbo.Slices.FiscalDate AS Expr3, dbo.Slices.RestaurantCode AS Expr4, dbo.Slices.Slices, dbo.[Gross Nett].Fiscaldate - 364 AS LastYearDate,
dbo.AndroRefunds.Amount
FROM dbo.[Gross Nett] LEFT OUTER JOIN
dbo.AndroRefunds ON dbo.[Gross Nett].Fiscaldate = dbo.AndroRefunds.FiscalDate AND
dbo.[Gross Nett].RestaurantCode = dbo.AndroRefunds.RestaurantCode LEFT OUTER JOIN
dbo.Slices ON dbo.[Gross Nett].Fiscaldate = dbo.Slices.FiscalDate AND dbo.[Gross Nett].RestaurantCode = dbo.Slices.RestaurantCode LEFT OUTER JOIN
dbo.Comps ON dbo.[Gross Nett].Fiscaldate = dbo.Comps.FiscalDate AND dbo.[Gross Nett].RestaurantCode = dbo.Comps.RestaurantCode
ORDER BY dbo.[Gross Nett].Fiscaldate, dbo.[Gross Nett].RestaurantCode
Hi Noreen,
Nice that the data is actually grouped already!
Simplified query to show this date last year. Now you might actually want to match wednesday with wednesday, not 23rd March with 23rd March which is the effect I've done.
HTH
David
PS The running total for the week-to-date I"ll pass on for now, other than to suggest building it in a temp table. Look for an article in SQL Server Central by Jeff Modem (sp) on the SQL Quirky Update or Solving the Running Total Problem.
Nice that the data is actually grouped already!
Simplified query to show this date last year. Now you might actually want to match wednesday with wednesday, not 23rd March with 23rd March which is the effect I've done.
HTH
David
PS The running total for the week-to-date I"ll pass on for now, other than to suggest building it in a temp table. Look for an article in SQL Server Central by Jeff Modem (sp) on the SQL Quirky Update or Solving the Running Total Problem.
select
g.Fiscaldate
, g.RestaurantCode
, g.Gross as ThisYearGross
, lastyear.Gross as LastYearGross
from dbo.[Gross Nett] g
inner join dbo.[Gross Nett] lastyear
on lastyear.Fiscaldate = dateadd( year, -1, g.Fiscaldate )
and lastyear.RestaurantCode = g.RestaurantCode
order by
g.Fiscaldate
, g.RestaurantCode
;
Hi David,
Might have missed something... Where did you get that the data is actually grouped already ?
Might have missed something... Where did you get that the data is actually grouped already ?
Hi Mark,
An assumption based on no group by clauses ... and inferred by the column and table names.
Regards
David
An assumption based on no group by clauses ... and inferred by the column and table names.
Regards
David
ASKER
Thanks for the comments. My Last Year Date is matching Wednesday with Wednesday not the 23rd with the 23rd
I wish to create a weekly running total PER STORE.
I'll check out the articles as suggested regarding the temp table etc.
Thanks
I wish to create a weekly running total PER STORE.
I'll check out the articles as suggested regarding the temp table etc.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Mark
Thanks for the suggestions and the updates. I going to close this one off, as have realised I can get what I need from a report.
Regards
Noreen
Thanks for the suggestions and the updates. I going to close this one off, as have realised I can get what I need from a report.
Regards
Noreen
If you are reporting on every single fiscal date, then not exactly sure what the running total "for every day" would be like...
Do you mean something like :
2011-01-01 RESTCODE01 $100.00 $100.00
2011-01-01 RESTCODE02 $100.00 $200.00
2011-01-01 RESTCODE03 $100.00 $300.00
2011-01-01 RESTCODE04 $100.00 $400.00
2011-01-02 RESTCODE01 $200.00 $200.00
2011-01-02 RESTCODE02 $200.00 $400.00
2011-01-02 RESTCODE02 $200.00 $600.00
2011-01-02 RESTCODE06 $200.00 $800.00
Also, not really seeing the calculation / aggregation of sales for "last year"
The challenge with this type of thing (especially the last year) is having the criteria match up exactly so that we can identify the target rows properly.
For example, looking at my small sample above, we will run into a problem for 2011-01-2 RESTCODE02 - the defining criteria in that sample is not unique enough so we might end up counting the restaurant multiple times
e.g. 2011-01-02 + RESTCODE02 the first time could give $400 (ie the first instance of $200 and the second $200 for that combo) , and then the second time we encounter it, again there is not enough to differentiate so we again get $400 - so our running total could be 200, 600, 1000, then back to 800
Did that make sense ?
Now I can see other columns in there but they are "optional" in so much as they are left outer joins, so might not be enough to differentiate which "row" is which...
Which means we might need to add in a differentiator that we can rely on - like a row_number() function.
For the comparable period last year, we have a similar problem...
I might be inclined to consider a "group by" fiscal date and restaurant code.
I would also use dateadd(yy,-1,fiscaldate) as the calculation for last year's date.
Is there any particular reason why it is a view (and has to use "order by" - normally use the order by when selecting from the view rather than having it in the view itself )