?
Solved

Grouping Data to Create a View

Posted on 1998-12-21
7
Medium Priority
?
170 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
tschill120198 earned 400 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
Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

 
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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

771 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