SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Get data month to calander week


Get data month to calander week

Author
Message
smer
smer
SSC-Enthusiastic
SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)

Group: General Forum Members
Points: 148 Visits: 218
Hello all,

I need two sample sqlserver queries

1. query get the data 1st date of the month to calendar week like week 1 , week 2 , week 3 etc.

2. Second I need a query example the query runs always before Thursday get the previous sum of week data but end date always Thursday?


Thanks
fkh
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26444 Visits: 17557
farrukhhameed786 (8/23/2013)
Hello all,

I need two sample sqlserver queries

1. query get the data 1st date of the month to calendar week like week 1 , week 2 , week 3 etc.

2. Second I need a query example the query runs always before Thursday get the previous sum of week data but end date always Thursday?


Thanks
fkh


You might start here. http://www.sqlservercentral.com/blogs/lynnpettis/2009/03/25/some-common-date-routines/

For the first one you would need to use a combination of the techniques outlined above and a calendar table (a quick search on this site will provide you with plenty of examples).

The second query above I don't understand what you want.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
smer
smer
SSC-Enthusiastic
SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)

Group: General Forum Members
Points: 148 Visits: 218
Thank you for reply I check the link for first issue and let you know. For second issue example my current week day is Thursday I look for example that query take the previous 7 days data from current week Thursday always
Snigdha Vartak
Snigdha Vartak
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 247
Code below will return Thursday from the current week for the @Date supplied. You can modify your where clause to use this date as FROM DATE and DATEADD(DAY,-7, THISDATE) as TO DATE

DECLARE @DATE AS DATETIME
SET @DATE='2013-08-28'

SELECT DATEADD(DAY, 5- DATEPART(dw, @DATE ), @DATE)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88090 Visits: 41128
farrukhhameed786 (8/23/2013)
Hello all,

I need two sample sqlserver queries

1. query get the data 1st date of the month to calendar week like week 1 , week 2 , week 3 etc.

2. Second I need a query example the query runs always before Thursday get the previous sum of week data but end date always Thursday?


Thanks
fkh


Since you're talking about Thursday, what day of the week does a week start on for you? Also, if a month doesn't start on the same day as the first day of the week and the fact the the first "week" of any given month could be as small a single day, what days of the week do you want to show for the first and last weeks of the month?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Ed Wagner
Ed Wagner
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16871 Visits: 10088
Jeff Moden (8/28/2013)

Since you're talking about Thursday, what day of the week does a week start on for you? Also, if a month doesn't start on the same day as the first day of the week and the fact the the first "week" of any given month could be as small a single day, what days of the week do you want to show for the first and last weeks of the month?


Jeff - You're always trying to get people to think through their requirements fully instead of encountering problems later. ;-)


Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
smer
smer
SSC-Enthusiastic
SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)SSC-Enthusiastic (148 reputation)

Group: General Forum Members
Points: 148 Visits: 218
I have attached a workbook to have a Idea how I need it.
Attachments
Book1.xlsx (25 views, 198.00 KB)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88090 Visits: 41128
Ed Wagner (8/29/2013)
Jeff Moden (8/28/2013)

Since you're talking about Thursday, what day of the week does a week start on for you? Also, if a month doesn't start on the same day as the first day of the week and the fact the the first "week" of any given month could be as small a single day, what days of the week do you want to show for the first and last weeks of the month?


Jeff - You're always trying to get people to think through their requirements fully instead of encountering problems later. ;-)


Heh... It's a "bad" habit of mine, huh? :-P I can't help it, though. I realize you can't correct a blank piece of paper but I've seen enough "bad paper" to know what should be included on the first blush and maybe even do a little mind reading to avoid foreseeable problems. :-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Snigdha Vartak
Snigdha Vartak
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 247
farrukhhameed786 (8/29/2013)
I have attached a workbook to have a Idea how I need it.


Let me work out something for you this long week end :-)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)SSC Guru (88K reputation)

Group: General Forum Members
Points: 88090 Visits: 41128
farrukhhameed786 (8/29/2013)
I have attached a workbook to have a Idea how I need it.


So... according to that partial spreadsheet (week 1 is missing a label as is the last week of 2013 so I have to make a guess), the following rules are what you want to follow...

1. Weeks always start on Thursdays.
2. The year that Wednesday falls in determines the year for that entire week.

Is that correct?

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search