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

Open in new window

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

Open in new window

Noreen McHughIT ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark WillsTopic AdvisorCommented:
Not sure what you mean by "I need to also show a week to date running total for every day on the Gross sale figure."

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 )
0
David ToddSenior DBACommented:
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.
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
;

Open in new window

0
Mark WillsTopic AdvisorCommented:
Hi David,

Might have missed something... Where did you get that the data is actually grouped already ?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

David ToddSenior DBACommented:
Hi Mark,

An assumption based on no group by clauses ... and inferred by the column and table names.

Regards
  David
0
Noreen McHughIT ManagerAuthor Commented:
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
0
Mark WillsTopic AdvisorCommented:
*smile* we should be able to help here as well...

using that much abbreviated test data I posted earlier, here are a couple of samples...

use tempdb
GO

if object_id('tempdb..#my_table','U') is not null drop table #my_table
GO

create table #my_table (FiscalDate datetime, restaurantcode char(10), gross money)
GO

-- some "current" year data
insert #my_table
SELECT '2011-01-01','restCODE01',100.00
UNION ALL SELECT '2011-01-01','restCODE02',100.00
UNION ALL SELECT '2011-01-01','restCODE03',100.00
UNION ALL SELECT '2011-01-01','restCODE04',100.00
UNION ALL SELECT '2011-01-02','restCODE01',200.00
UNION ALL SELECT '2011-01-02','restCODE02',200.00
UNION ALL SELECT '2011-01-02','restCODE02',200.00
UNION ALL SELECT '2011-01-02','restCODE06',200.00

-- and last (calendar) year data
insert #my_table
SELECT '2010-01-01','restCODE01',100.00
UNION ALL SELECT '2010-01-01','restCODE02',100.00
UNION ALL SELECT '2010-01-01','restCODE03',100.00
UNION ALL SELECT '2010-01-01','restCODE04',100.00
UNION ALL SELECT '2010-01-02','restCODE01',200.00
UNION ALL SELECT '2010-01-02','restCODE02',200.00
UNION ALL SELECT '2010-01-02','restCODE02',200.00
UNION ALL SELECT '2010-01-02','restCODE06',200.00
GO

-- ok, first sample is using WINDOW FUNCTIONS

select    year(t.fiscaldate) as yr
        , datepart(week,t.fiscaldate) as wk
        , t.restaurantcode
        , t.fiscaldate
        , t.gross
        , sum(t.gross) over (partition by t.restaurantcode,year(t.fiscaldate),datepart(week,t.fiscaldate)) as week_running_total
        , l.gross as last_years_gross
from #my_table T
left outer join #my_table L on l.restaurantcode = t.restaurantcode and l.fiscaldate = dateadd(day,-364,t.fiscaldate)
order by 1,2,3,4
Go

-- Next we do a more traditional (?) link back with a unique row_number identifier

;with my_running_totals as
(select *
        , datepart(year,fiscaldate) yr
        , datepart(week,fiscaldate) wk
        , row_number() over (order by restaurantcode,fiscaldate ) as rn
        , dateadd(day,-364,fiscaldate) as ly
 from #my_table)
select t.yr, t.wk, t.restaurantcode, t.fiscaldate, t.gross, sum(r.gross) as week_running_total, max(l.gross) as last_years_gross
from my_running_totals t
inner join my_running_totals r on r.restaurantcode = t.restaurantcode and r.yr = t.yr and r.wk = t.wk and r.rn <= t.rn
left outer join my_running_totals l on l.restaurantcode = t.restaurantcode and l.fiscaldate = t.ly
group by t.restaurantcode, t.yr, t.wk, t.fiscaldate, t.rn, t.gross
order by  t.yr, t.wk, t.restaurantcode, t.fiscaldate, t.rn
Go

Open in new window


Now, there are obviously a few gotcha's in that sample code (by design), so maybe we can discuss...
1) definition of "week"
2) is it already grouped so that multiple instances of restaurant dont exist per fiscaldate
3) can you post some sample data with expected results ? (obfuscate anything sensitive)
4) do you want me to continue ?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Noreen McHughIT ManagerAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.