Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
adiel
Starting Member
16 Posts |
Posted - 2008-04-07 : 13:04:51
|
| I have stumbled upon a sql question I cannot answer. I am looking for the following SELECT sql statement:Basically I need a way to get "5 days ago from today". BUT, the trick is that there is a table called tblnoworkday with contains weekends and holidays and those dates cannot count. So basically what I am really trying to get is "5 Business days ago from today". So it would basically do this for an query input date of '4/9/08'. If the table is called TblNoWorkday and contains the following records:...2008-04-06 00:00:00.0002008-04-05 00:00:00.000...This is the last 5 business days then: (not in the table)4/9/084/8/084/7/08 ****weekend****4/4/084/3/08The query should return just 4/3/08. The problem is 4/3/08 doesn't exist in the database since the database only contains the no work days.Any ideas on how to do this in a simple query without having to create another table with the valid working dates?Thanks Before Hand,Adiel |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-07 : 13:15:52
|
| select top 5 d = dateadd(dd,i.i*-1,dte.d)from (select i = 0 union all select 1 union select 2 ... union select 10) icross join (select d = dateadd(dd,0,getdate())) dteleft join tblnoworkday non dateadd(dd,i.i*-1,dte.d) = n.dtewhere n.dte is nullorder by dte.d desc==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
adiel
Starting Member
16 Posts |
Posted - 2008-04-07 : 13:45:04
|
| Thanks! I ran into two issues with your query. I changed it as follows:select top 5 d = dateadd(dd,i.i*-1,dte.d)from (select i = 0 union all select 1 union select 2 union select 3 union select 4 union select 5union select 6 union select 7 union select 8 union select 9 union select 10) icross join (select d = dateadd(dd,0,getdate())) dteleft join tblnoworkday non dateadd(dd,i.i*-1,dte.d) = n.noworkdaywhere n.noworkday is nullorder by dte.d desc1. It still returns all dates for some reason. Here is the output. (I changed the column name to match my column name. The column noworkday is datetime)2008-04-07 13:40:50.0232008-04-06 13:40:50.0232008-04-05 13:40:50.0232008-04-04 13:40:50.0232008-04-03 13:40:50.0232. How can it be modified to just return the 5th date. (It would be 4/3/08 if you see my example above.)Thanks Again,Adiel |
 |
|
|
bfoster
Starting Member
30 Posts |
Posted - 2008-04-07 : 14:21:31
|
| Here's a slightly different solution using a common table expression. You may be able to apply the top(1) idea that I used below to the other query if you want to stick with the other query.WITH dte AS(SELECT CAST(CONVERT(varchar, getdate(), 101) AS DATETIME) AS dUNION ALLSELECT DATEADD(day, -1, d )FROM dteWHERE d >= DATEADD(day, -15, getdate()))SELECT top(1) a.dFROM(SELECT top(5) dte.dFROM dteleft join tblnoworkday n on dte.d = n.noworkdaywhere n.noworkday is nullorder by dte.d desc) aorder by a.d asc; |
 |
|
|
adiel
Starting Member
16 Posts |
Posted - 2008-04-07 : 14:30:40
|
| Thanks bfoster, I received the following error when I ran your code:Server: Msg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'WITH'.Server: Msg 170, Level 15, State 1, Line 9Line 9: Incorrect syntax near '('.Server: Msg 170, Level 15, State 1, Line 12Line 12: Incorrect syntax near '('.I am testing the code on sql server 2000. Thanks Again,Adiel |
 |
|
|
adiel
Starting Member
16 Posts |
Posted - 2008-04-07 : 14:59:13
|
| It looks like you are expecting an object called dte that does not exist:Invalid object name 'dte'. |
 |
|
|
bfoster
Starting Member
30 Posts |
Posted - 2008-04-07 : 15:27:44
|
| Common table expression are new to SQL Server 2005 so you won't be able to use that syntax in SQL Server 2000. That's the problem there.In that case, use the original suggestion.select top(1) dfrom ([first suggestion given]) aorder by d ascMy only other thought is you may need to look into the time part of the dates that you are trying to compare and make sure they are all 00:00:00.000 if that's what is in your tblnoworkday. |
 |
|
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2008-04-07 : 16:49:32
|
| Sorry - got the date bit wrongselect top 1 *from(select top 5 d = dateadd(dd,i.i*-1,dte.d)from (select i = 0 union all select 1 union select 2 union select 3 union select 4 union select 5union select 6 union select 7 union select 8 union select 9 union select 10) icross join (select d = convert(datetime,datediff(dd,0,getdate()))) dteleft join tblnoworkday non dateadd(dd,i.i*-1,dte.d) = n.noworkdaywhere n.noworkday is nullorder by dte.d desc) aorder by dmight be better asselect top 1 *from(select top 5 d = dte.dfrom(select d = dte.d - ifrom (select i = 0 union all select 1 union select 2 union select 3 union select 4 union select 5union select 6 union select 7 union select 8 union select 9 union select 10) icross join (select d = convert(datetime,datediff(dd,0,getdate()))) dte) dteleft join tblnoworkday non dte.d = n.noworkdaywhere n.noworkday is nullorder by dte.d desc) aorder by d==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
adiel
Starting Member
16 Posts |
Posted - 2008-04-08 : 08:59:08
|
| Thanks Everyone!! Peso, it was not a cross-post, it was a move-post. I posted in 2005 but it was supposed to be sql 2000.>> am testing the code on sql server 2000. Thanks Again, |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-04-08 : 09:02:05
|
[code]SELECT TOP 5 DATEADD(DAY, d.theDay, 0) AS theDayFROM ( SELECT DATEDIFF(DAY, 0, GETDATE()) AS theDay UNION ALL SELECT DATEDIFF(DAY, 1, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 2, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 3, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 4, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 5, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 6, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 7, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 8, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 9, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 10, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 11, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 12, GETDATE()) UNION ALL SELECT DATEDIFF(DAY, 13, GETDATE()) ) AS d LEFT JOIN tblNoWorkDay AS x ON x.SomeDay = d.theDayWHERE x.SomeDay IS NULLORDER BY d.theDay DESC[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|
|