Solved

Grouping Data to Create a View

Posted on 1998-12-21
7
161 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 2

Expert Comment

by:tschill120198
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
doh! I didn't think about changing the second date.
Thanks for all of your help.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now