Solved

Grouping dates by week

Posted on 2012-03-26
3
463 Views
Last Modified: 2012-03-27
I have a field called FieldA which has a date value.  I would like to group the dates by week. Week1 - Week52.  

What syntax can I use to assign a week number to each respective date.
We will use Sunday as the first day of the week and Saturday as the last day of the week.
0
Comment
Question by:morinia
[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
3 Comments
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 37768735
you can use this for getting the week number

select to_char(sysdate,'IW') from dual;

and use the same thing in group by also..
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 37769411
Assuming SQL Server (for Oracle see previous answer):

select datepart(week, @dtDateValue) will return an integer corresponding to the ISO week number (week 1 is the week containing the first Thursday in the year; weeks start on Sunday).

Thus try grouping on datepart(week, FieldA):

select datepart(week, FieldA) as 'WeekNr', OtherField1, OtherField2, .....
from TableName
group by datepart(week, FieldA)

should give you the results with the week number at the front.

(Apologies for any typos - I don't have a copy of SQL Server in front of me to check things with)

Mike
0
 
LVL 11

Accepted Solution

by:
theartfuldazzler earned 500 total points
ID: 37769958
Hi Morinia

I know you have asked SAS questions before, so here is the SAS solution:

DATA Input_DS;
  Date = '01Jan2012'd;
  OUTPUT;
  DATE = '31Dec2011'd;
  OUTPUT;
  DATE = '29JUL2009'd;
  OUTPUT;
  FORMAT Date Date9.;
RUN;

DATA Output_DS;
  Set Input_DS;
    Start_of_year = MDY(1,1, YEAR(Date));
	Week_no = INTCK("WEEK",Start_of_year,Date)+1;  
	/* One might need to make adjustments to the +1 depending on how one counts the first week */
	DROP Start_of_year;
RUN;

Open in new window

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

These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

729 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