Solved

Grouping Data to Create a View

Posted on 1998-12-21
7
167 Views
Last Modified: 2010-03-19
I have a table that has sales data for different locations, one record per location for each day:

location_num  date      amount1 amount2
1             12/7/98   100.00  10.00
1             12/8/98   150.00   5.00
.
.
1             12/13/98  100.00  10.00
2             12/7/98    50.00   5.00
.
.
2             12/13/98   120.00  12.00


I want to create a view that sums the amounts for each week for each location:

location_num  week_ending amount1 amount2
1             12/13/98     700.00  80.00
2             12/13/98     650.00  25.00

I know how to create a select statement when I have the beginning and ending date for the week:
select location_num, sum(amount1), sum(amount2) from xyz where date between '12/7/98' and '12/13/98'.

However, I want to create a view that will sum up the sales for each week without hardcoding any dates into it; I just want to specify that a week is Monday - Sunday and have Sunday show as the week ending date.

I'm not sure this is possible in a view.  I know that I can create a table, but I don't want to worry about it getting out of sync (which is actually the situation I am in right now). Does anyone have any ideas how to go about this?
0
Comment
Question by:trillian30
  • 4
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
tschill120198 earned 100 total points
ID: 1092341
This will do most of what you want... one limitation is that it only prints the last day that has an entry for each week, rather than the last day of the week; not a problem if every day has an entry.  

create view WeeklyTotal
as
    select  location_num,
            max(date) as EndingDate,
            sum(amount1) as Amount1,
            sum(amount2) as Amount2
    from    test
    group by location_num,
            datediff(wk, '1/7/1900', date),
            datediff(yy, '1/1/1900', date)

0
 
LVL 2

Expert Comment

by:tschill120198
ID: 1092342
Scratch the last datediff in the "group by"... it isn't needed, and using it creates a problem when a week crosses a change in years (i.e., 1998 -1999).  
0
 

Author Comment

by:trillian30
ID: 1092343
Thank you for your help tschill,  just one question:
What is the significance of '1/7/1900'? In general I understand how the datediff function works, but I don't understand why using '1/7/1900' works.  What would I use if I want my week ending to be on a different day? Sorry, to be such a newbie!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:tschill120198
ID: 1092344
No problem... There is nothing significant about 1/7/1900.  I choose it starting point for weeks, figuring it would be before any of the dates in your table, and not knowing if it being a Sunday made a difference... don't think it does (any date in that same week would been OK).  I'd have to test a bit more, though.

If you want to use a different day for a week ending you'd probably have to do a dateadd() to adjust the dates, since it appears that "wk" in datediff goes from Sunday-Saturday.
0
 

Author Comment

by:trillian30
ID: 1092345
I'm not sure where to use the dateadd.  If I add days to '1/7/1900' it gives me the same result (Sun. to Sat.).

I'm really making you work for the points, aren't I? :)

0
 
LVL 2

Expert Comment

by:tschill120198
ID: 1092346
I haven't tested the effect on the rest of the query, but the idea is that adding (-1) days to the second date in the datediff function will move the start day from Sunday to Monday... the following code uses the same four dates, but in the first set the week number changes on Sunday, in the second set it changes on Monday.

set nocount on
select 'Sunday - Saturday'
select datediff(wk, '1/7/1900', '12/18/1998') as 'Friday',
      datediff(wk, '1/7/1900', '12/19/1998') as 'Saturday',
      datediff(wk, '1/7/1900', '12/20/1998') as 'Sunday',
      datediff(wk, '1/7/1900', '12/21/1998') as 'Monday'
select 'Monday - Sunday'
select datediff(wk, '1/7/1900', dateadd(dd, -1, '12/18/1998')) as 'Friday',
      datediff(wk, '1/7/1900', dateadd(dd, -1, '12/19/1998')) as 'Saturday',
      datediff(wk, '1/7/1900', dateadd(dd, -1, '12/20/1998')) as 'Sunday',
      datediff(wk, '1/7/1900', dateadd(dd, -1, '12/21/1998')) as 'Monday'

0
 

Author Comment

by:trillian30
ID: 1092347
doh! I didn't think about changing the second date.
Thanks for all of your help.

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question