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

x
?
Solved

Grouping dates by week

Posted on 2012-03-26
3
Medium Priority
?
470 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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

604 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