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 |
Windza
Yak Posting Veteran
61 Posts |
Posted - 2008-09-23 : 21:39:49
|
Hi all...Regarding the following statement, SELECT a.WeekOfYear, SUM(b.WeekTotals) AS HistBacklogFROM(SELECT DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)) AS WeekOfYear,COUNT(RaisedDateTime)-COUNT(FinishedDateTime) AS 'WeekTotals'FROM dbo.WorkOrder GROUP BY DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)))AS a INNER JOIN (SELECT DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)) AS WeekOfYear,COUNT(RaisedDateTime)-COUNT(FinishedDateTime) AS 'WeekTotals'FROM dbo.WorkOrder GROUP BY DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)))AS bON a.WeekOfYear >= b.WeekOfYearWHERE a.WeekOfYear BETWEENDATENAME(wk, GETDATE()-100)+' '+DATENAME(yyyy, GETDATE()-100) ANDDATENAME(wk, GETDATE())+' '+DATENAME(yyyy, GETDATE())GROUP BY a.WeekOfYearThe results are fine when I use the GETDATE() command in the WHERE clause - but when I attempt to utilise user specified dates ([Date1Start] and [Date1End]) there are no results returned...The user fields are regular datetime format and have worked fine with other statements... I'm hoping there is an easy explanation (possibly my structure) for this...As always - your help is appreciated.Cheers... |
|
jobejufranz
Starting Member
33 Posts |
Posted - 2008-09-24 : 00:50:17
|
quote: Originally posted by Windza Hi all...Regarding the following statement, SELECT a.WeekOfYear, SUM(b.WeekTotals) AS HistBacklogFROM(SELECT DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)) AS WeekOfYear,COUNT(RaisedDateTime)-COUNT(FinishedDateTime) AS 'WeekTotals'FROM dbo.WorkOrder GROUP BY DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)))AS a INNER JOIN (SELECT DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)) AS WeekOfYear,COUNT(RaisedDateTime)-COUNT(FinishedDateTime) AS 'WeekTotals'FROM dbo.WorkOrder GROUP BY DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)))AS bON a.WeekOfYear >= b.WeekOfYearWHERE a.WeekOfYear BETWEENDATENAME(wk, GETDATE()-100)+' '+DATENAME(yyyy, GETDATE()-100) ANDDATENAME(wk, GETDATE())+' '+DATENAME(yyyy, GETDATE())GROUP BY a.WeekOfYearThe results are fine when I use the GETDATE() command in the WHERE clause - but when I attempt to utilise user specified dates ([Date1Start] and [Date1End]) there are no results returned...The user fields are regular datetime format and have worked fine with other statements... I'm hoping there is an easy explanation (possibly my structure) for this...As always - your help is appreciated.Cheers...
Are you passing the user specified date to a string variable and construct your dynamic query? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-24 : 01:29:08
|
How are you passing user defined dates?can you show query with date params? |
 |
|
Windza
Yak Posting Veteran
61 Posts |
Posted - 2008-09-24 : 01:42:26
|
The statement with the date parameters is simply as follows...SELECT a.WeekOfYear, SUM(b.WeekTotals) AS HistBacklogFROM(SELECT DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)) AS WeekOfYear,COUNT(RaisedDateTime)-COUNT(FinishedDateTime) AS 'WeekTotals'FROM dbo.WorkOrder GROUP BY DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)))AS a INNER JOIN (SELECT DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)) AS WeekOfYear,COUNT(RaisedDateTime)-COUNT(FinishedDateTime) AS 'WeekTotals'FROM dbo.WorkOrder GROUP BY DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)))AS bON a.WeekOfYear >= b.WeekOfYearWHERE a.WeekOfYear BETWEENDATENAME(wk, [Date1Start])+' '+DATENAME(yyyy, [Date1Start]) ANDDATENAME(wk, [Date1End])+' '+DATENAME(yyyy, [Date1End])GROUP BY a.WeekOfYear |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-24 : 01:53:57
|
quote: Originally posted by Windza The statement with the date parameters is simply as follows...SELECT a.WeekOfYear, SUM(b.WeekTotals) AS HistBacklogFROM(SELECT DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)) AS WeekOfYear,COUNT(RaisedDateTime)-COUNT(FinishedDateTime) AS 'WeekTotals'FROM dbo.WorkOrder GROUP BY DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)))AS a INNER JOIN (SELECT DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)) AS WeekOfYear,COUNT(RaisedDateTime)-COUNT(FinishedDateTime) AS 'WeekTotals'FROM dbo.WorkOrder GROUP BY DATENAME(wk, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103))+' '+DATENAME(yyyy, CONVERT(datetime, dbo.WorkOrder.RaisedDateTime, 103)))AS bON a.WeekOfYear >= b.WeekOfYearWHERE a.WeekOfYear BETWEENDATENAME(wk, [Date1Start])+' '+DATENAME(yyyy, [Date1Start]) ANDDATENAME(wk, [Date1End])+' '+DATENAME(yyyy, [Date1End])GROUP BY a.WeekOfYear
the between wont work as expected you're giving two string value as start and end range.What will be contents of WeekofYear column? |
 |
|
Windza
Yak Posting Veteran
61 Posts |
Posted - 2008-09-24 : 02:13:00
|
The WeekOfYear is week and year in the following format12 200813 200814 2008etc. |
 |
|
Windza
Yak Posting Veteran
61 Posts |
Posted - 2008-09-24 : 02:24:43
|
quote: Originally posted by visakh16the between wont work as expected you're giving two string value as start and end range.What will be contents of WeekofYear column?
While I'm not disputing what you say (it makes sense), doesn't it seem odd then that with a GETDATE() parameter it works? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-24 : 02:25:53
|
quote: Originally posted by Windza The WeekOfYear is week and year in the following format12 200813 200814 2008etc.
then useCAST(RIGHT(a.WeekOfYear,4)+LEFT(a.WeekOfYear,2) AS int)>=CAST(DATENAME(yyyy, [Date1Start])+DATENAME(wk, [Date1Start])AS int)AND CAST(RIGHT(a.WeekOfYear,4)+LEFT(a.WeekOfYear,2) AS int)>=CAST(DATENAME(yyyy, [Date1End])+DATENAME(wk, [Date1End])AS int) instead of a.WeekOfYear BETWEENDATENAME(wk, [Date1Start])+' '+DATENAME(yyyy, [Date1Start]) ANDDATENAME(wk, [Date1End])+' '+DATENAME(yyyy, [Date1End]) |
 |
|
Windza
Yak Posting Veteran
61 Posts |
Posted - 2008-09-24 : 03:40:41
|
quote: then useCAST(RIGHT(a.WeekOfYear,4)+LEFT(a.WeekOfYear,2) AS int)>=CAST(DATENAME(yyyy, [Date1Start])+DATENAME(wk, [Date1Start])AS int)AND CAST(RIGHT(a.WeekOfYear,4)+LEFT(a.WeekOfYear,2) AS int)>=CAST(DATENAME(yyyy, [Date1End])+DATENAME(wk, [Date1End])AS int)
Visakh16 - I'm sure you've given me the correct functionality with the statement but is the latter 'greater than or equal' condition (>=)correct or should it be 'less than or equal' (<=)?What I need is Date1Start <= WeekOfYear <= Date1End (I may not have been clear in this). I thought it would be just a simple matter of reversing this particular sign but then I don't get any results... |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-24 : 03:43:14
|
quote: Originally posted by Windza
quote: then useCAST(RIGHT(a.WeekOfYear,4)+LEFT(a.WeekOfYear,2) AS int)>=CAST(DATENAME(yyyy, [Date1Start])+DATENAME(wk, [Date1Start])AS int)AND CAST(RIGHT(a.WeekOfYear,4)+LEFT(a.WeekOfYear,2) AS int)>=CAST(DATENAME(yyyy, [Date1End])+DATENAME(wk, [Date1End])AS int)
Visakh16 - I'm sure you've given me the correct functionality with the statement but is the latter 'greater than or equal' condition (>=)correct or should it be 'less than or equal' (<=)?What I need is Date1Start <= WeekOfYear <= Date1End (I may not have been clear in this). I thought it would be just a simple matter of reversing this particular sign but then I don't get any results...
that was a copy paste mistake.it should be <= . |
 |
|
Windza
Yak Posting Veteran
61 Posts |
Posted - 2008-09-24 : 03:47:37
|
No worries - but the trouble is now that I have reversed the sign I don't have any results at all... |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-24 : 03:53:37
|
quote: Originally posted by Windza No worries - but the trouble is now that I have reversed the sign I don't have any results at all...
do you always have WeekNumber in format wk yyyy? all this complications happen only because you have character field for storing week and year. You might have used date field or even integer to store this to make manipulations easier. |
 |
|
Windza
Yak Posting Veteran
61 Posts |
Posted - 2008-09-24 : 03:57:55
|
quote: do you always have WeekNumber in format wk yyyy? all this complications happen only because you have character field for storing week and year. You might have used date field or even integer to store this to make manipulations easier.
I'll go back through everything... interestingly enough, when I remove AND CAST(RIGHT(a.WeekOfYear,4)+LEFT(a.WeekOfYear,2) AS int)>=CAST(DATENAME(yyyy, [Date1End])+DATENAME(wk, [Date1End])AS int) from the WHERE clause I get results... I'll keep delving.Anyway - I certainly appreciate you putting me on the right track ... many thanks again ! |
 |
|
Windza
Yak Posting Veteran
61 Posts |
Posted - 2008-09-24 : 04:06:48
|
I discovered the problem, thanks Visakh16... was just as you suggested (I had CONVERT on the date parameters in the initial SELECT clause).Cheers... |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-24 : 04:11:19
|
quote: Originally posted by Windza I discovered the problem, thanks Visakh16... was just as you suggested (I had CONVERT on the date parameters in the initial SELECT clause).Cheers...
welcome |
 |
|
|
|
|
|
|