| Author |
Topic |
|
AsimKhaliq
Yak Posting Veteran
94 Posts |
Posted - 2003-11-05 : 21:02:44
|
| hiCan anyone help me in writing 3 different queries which retrives1)all record from a table for previous week 2)all record from a table for previous quarter3)all record from a table for previous monthNote the start day of month is not sunday it may be monday, tuesday or whatever the admin will setThanks in advance |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2003-11-05 : 21:45:48
|
| You mentioned the starting day is variable, what about the end day?Is the assumption for week 7 days or 5 days? ie M-F or a through b where a and b are input params?Previous month meaning 1st of the month through last of the month or again a to b?You mention the admin will set the day - Can you be more specific ? |
 |
|
|
AsimKhaliq
Yak Posting Veteran
94 Posts |
Posted - 2003-11-05 : 22:39:54
|
| hey thanksI got the months and week thing but stuck in previous quater will u please help me and previous quater means one minus he current quaterthanks |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2003-11-05 : 22:42:59
|
Sure, DECLARE @thisdate datetime, @startdate datetime, @enddate datetimeSET @thisdate = convert(datetime,convert(varchar(12),getdate()))--Previous Quarter--Assumes Q1 = 1/1 - 4/1 ; Q2 = 4/1 - 7/1 ; Q3 = 7/1 - 10/1 ; Q4 = 10/1 - 1/1select @startdate = case when @thisdate between convert(datetime,'1/1/' + convert(char(4),datepart(yy,getdate()))) AND convert(datetime,'4/1/' + convert(char(4),datepart(yy,getdate()))) --Forth Quarter of last year then convert(datetime,'10/1/' + convert(char(4),datepart(yy,getdate())-1)) when @thisdate between convert(datetime,'4/1/' + convert(char(4),datepart(yy,getdate()))) AND convert(datetime,'7/1/' + convert(char(4),datepart(yy,getdate()))) --First Quarter of tis year then convert(datetime,'1/1/' + convert(char(4),datepart(yy,getdate()))) when @thisdate between convert(datetime,'7/1/' + convert(char(4),datepart(yy,getdate()))) AND convert(datetime,'10/1/' + convert(char(4),datepart(yy,getdate()))) --Second Quarter of this year then convert(datetime,'4/1/' + convert(char(4),datepart(yy,getdate()))) when @thisdate between convert(datetime,'10/1/' + convert(char(4),datepart(yy,getdate()))) AND convert(datetime,'1/1/' + convert(char(4),datepart(yy,getdate())+1)) --Third Quarter of this Year then convert(datetime,'7/1/' + convert(char(4),datepart(yy,getdate()))) end, @enddate = dateadd(mm,3,@startdate)select @startdate StartofLastQ,@enddate EndOfLastQ |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2003-11-05 : 22:48:12
|
| Ummmmm...SELECT * FROM myTable WHERE StartDate BETWEEN DateAdd(q, DateDiff(q, 0, Start), 0) AND DateAdd(q, DateDiff(q, 0, Start)+1, 0)Shit, I goofed that up, this is the correct version:SELECT * FROM myTable WHERE StartDate BETWEEN DateAdd(q, DateDiff(q, 0, Start)-1, 0) AND DateAdd(q, DateDiff(q, 0, Start), 0) |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2003-11-05 : 22:57:41
|
quote: Originally posted by robvolk Ummmmm...SELECT * FROM myTable WHERE StartDate BETWEEN DateAdd(q, DateDiff(q, 0, Start), 0) AND DateAdd(q, DateDiff(q, 0, Start)+1, 0)
Well I think we'll all agree that code is much better...Except s/he was looking for prev qSELECT * from table WHERE StartDate Between DateAdd(q, DateDiff(q, 0, dateadd(mm,-3,getdate())), 0) AND DateAdd(q, DateDiff(q, 0, dateadd(mm,-3,getdate()))+1, 0) |
 |
|
|
|
|
|