TSQL Taking a Weekly Number and spreading it across the days of the week

I have two tables. One Table has Weekly Sales by Store and by Week. Our week starts on a Saturday and ends on a Friday. The second table defines the week and all the dates within it.

Is there a way to use both tables so I can come up with average sales across all the different days. Sales differ by week and so the daily average would be different if spread across the days.

The use case of this dataset would be to upload it to another system that only accepts sales by day instead of week.

I have uploaded an example. Columns A-C represent the first table. Columns E-F represent the second table. Columns H-J represent the results I am looking to achieve.
Example.xlsx
Ryan SimmonsBusiness Analyst IIIAsked:
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.

ste5anSenior DeveloperCommented:
Well, you need a calendar table, e.g.

SET DATEFIRST 6; -- Only required when not already the default.

DECLARE @WeekylSales TABLE
(
    YearNo INT NOT NULL,
    WeekNo INT NOT NULL,
    SalesAmount DECIMAL(18,2) NOT NULL
);

INSERT INTO @WeekylSales
VALUES  ( 2018, 1, 100 ),
        ( 2018, 2, 200 );

WITH Calendar AS
    (
        SELECT  DATEADD(DAY, ROW_NUMBER() OVER ( ORDER BY A.object_id) - 1, '20000101') AS DayDate,
                YEAR(DATEADD(DAY, ROW_NUMBER() OVER ( ORDER BY A.object_id) - 1, '20000101')) AS YearNo,
                DATEPART(ISO_WEEK, DATEADD(DAY, ROW_NUMBER() OVER ( ORDER BY A.object_id) - 1, '20000101')) AS IsoWeekNo,
                DATEPART(WEEK, DATEADD(DAY, ROW_NUMBER() OVER ( ORDER BY A.object_id) - 1, '20000101')) AS WeekNo
        FROM    sys.objects A, sys.objects
    )
    SELECT  *,
            S.SalesAmount / COUNT(*) OVER ( PARTITION BY C.YearNo, C.WeekNo )
    FROM    Calendar C
        INNER JOIN @WeekylSales S ON C.YearNo = S.YearNo
                                    AND C.WeekNo = S.WeekNo;

Open in new window

1

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
Snarf0001Commented:
It looks like you already have the calendar table, and you're just looking to combine the two of them.
In which case you can just use a windowed count:

select ws.Store, wd.Date, ws.Sales / count(1) over (partition by Store, LaborWeek) as AvgSales
from WeeklySales ws
full join WeekDays wd on ws.LaborWeek = wd.Week

Open in new window

1
Mark WillsTopic AdvisorCommented:
select store, date, sales / count(*) over (partition by store,week)
from table1
inner join table2 on laborWeek = week 

Open in new window


Would like to know datatypes for dates
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mark WillsTopic AdvisorCommented:
Some explanation of my submission...

1) used the names in the spreadsheet - so needs some work there
2) assumed date (or zero time in datetime) datatypes - critical for the join
3) sales divided by the count (ie count(*)) of rows returned for that store + week (in table 2)
4) need to assume that the table2 might not be complete for all dates and so have to partition by the number of rows for the store + week combination.
5) there are a few keywords being used and should be enclosed in square brackets.

More than happy to resubmit if you can see any contradictions in the above assumptions...
0
Snarf0001Commented:
The same as I submitted ;)

Two small changes if you want to merge with Mark's any my own:

1) Use the "inner join" Mark has instead of the full in mine, that was a mistake (whoops)
2) As a general rule, use count(1) instead of count(*)
0
ste5anSenior DeveloperCommented:
@Snarf: I strongly disagree with 2).. This is is just a matter of coding style.
0
Ryan SimmonsBusiness Analyst IIIAuthor Commented:
Thank you! I was able to build a solution mainly using @Ste5an's answers but also by tweaking the code using @Snarf0001 and @MarkWillis answers.
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
Sales

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.