Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Grouping Data to Create a View

Posted on 1998-12-21
7
Medium Priority
?
171 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
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

636 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