September 26, 2012 at 8:48 am
Hello,
I want to add filter NextReviewDate to the query in the where clause, how come this doesn't work? It says something wrong at the first case
where d.SA_ID like case when @SA_ID <> 0 then @SA_ID else d.SA_ID end and d.doctype = 1
and
d.ciodivisionid like case when @DivisionID <> 0 then @DivisionID else d.ciodivisionid end
and
case When @Due = 'Over Due' then NextReviewDate < getDate()
case When @Due = 'Due In 30 Days' then NextReviewDate >= getDate() And NextReviewDate < getDate() + 30
case When @Due = 'Due In 60 Days' then NextReviewDate >= getDate() + 30 And NextReviewDate < getDate() + 60
case When @Due = 'Due In 90 Days' then NextReviewDate >= getDate() + 60 And NextReviewDate < getDate() + 90
case When @Due = 'Due In 180 Days' then NextReviewDate >= getDate() + 90 And NextReviewDate < getDate() + 180
else
@Due = @Due
end
Thanks.
September 26, 2012 at 9:05 am
a case can't due logic...it must return a single value; you are trying to use it more like an If statement to get NextReviewDate < getDate() for example.
i think it's more like this:
...
where d.SA_ID like case
when @SA_ID <> 0
then @SA_ID
else d.SA_ID
end
and d.doctype = 1
and d.ciodivisionid like case
when @DivisionID <> 0
then @DivisionID
else d.ciodivisionid
end
and NextReviewDate BETWEEN case
When @Due = 'Over Due'
then 0
ELSE getDate()
END
AND case
When @Due = 'Over Due'
then getDate()
When @Due = 'Due In 30 Days'
THEN getDate() + 30
When @Due = 'Due In 60 Days'
THEN getDate() + 60
When @Due = 'Due In 90 Days'
THEN getDate() + 90
When @Due = 'Due In 180 Days'
THEN getDate() + 180
END
Lowell
September 26, 2012 at 9:16 am
Something similar to Lowell's but I believe he missed something.
AND NextReviewDate >= CASE WHEN @Due = 'Over Due' THEN '19000101'
WHEN @Due = 'Due In 30 Days' THEN GETDATE()
WHEN @Due = 'Due In 60 Days' THEN DATEADD( dd, 30, GETDATE())
WHEN @Due = 'Due In 90 Days' THEN DATEADD( dd, 60, GETDATE())
WHEN @Due = 'Due In 180 Days' THEN DATEADD( dd, 90, GETDATE())
ELSE '19000101' END
AND NextReviewDate < CASE WHEN @Due = 'Over Due' THEN GETDATE()
WHEN @Due = 'Due In 30 Days' THEN DATEADD( dd, 30, GETDATE())
WHEN @Due = 'Due In 60 Days' THEN DATEADD( dd, 60, GETDATE())
WHEN @Due = 'Due In 90 Days' THEN DATEADD( dd, 90, GETDATE())
WHEN @Due = 'Due In 180 Days' THEN DATEADD( dd, 180, GETDATE())
ELSE '29991231' END
September 26, 2012 at 9:23 am
(CROSS) APPLY is ideal for this:
CROSS APPLY (
SELECT DueStatus =
case
When NextReviewDate < getDate() then 'Over Due'
When NextReviewDate >= getDate() And NextReviewDate < getDate() + 30 then 'Due In 30 Days'
When NextReviewDate >= getDate() + 30 And NextReviewDate < getDate() + 60 then 'Due In 60 Days'
When NextReviewDate >= getDate() + 60 And NextReviewDate < getDate() + 90 then 'Due In 90 Days'
When NextReviewDate >= getDate() + 90 And NextReviewDate < getDate() + 180 then 'Due In 180 Days'
ELSE NULL END
) x
where d.SA_ID like case when @SA_ID <> 0 then @SA_ID else d.SA_ID end and d.doctype = 1
and d.ciodivisionid like case when @DivisionID <> 0 then @DivisionID else d.ciodivisionid end
and x.dueStatus = @Due
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 26, 2012 at 9:42 am
ChrisM@Work (9/26/2012)
(CROSS) APPLY is ideal for this:
CROSS APPLY (
SELECT DueStatus =
case
When NextReviewDate < getDate() then 'Over Due'
When NextReviewDate >= getDate() And NextReviewDate < getDate() + 30 then 'Due In 30 Days'
When NextReviewDate >= getDate() + 30 And NextReviewDate < getDate() + 60 then 'Due In 60 Days'
When NextReviewDate >= getDate() + 60 And NextReviewDate < getDate() + 90 then 'Due In 90 Days'
When NextReviewDate >= getDate() + 90 And NextReviewDate < getDate() + 180 then 'Due In 180 Days'
ELSE NULL END
) x
where d.SA_ID like case when @SA_ID <> 0 then @SA_ID else d.SA_ID end and d.doctype = 1
and d.ciodivisionid like case when @DivisionID <> 0 then @DivisionID else d.ciodivisionid end
and x.dueStatus = @Due
Thanks for all replies, I like this because it is closest to my original, it gets validated however, running it pops error:
Incorrect syntax near the keyword 'Over'.
September 26, 2012 at 9:44 am
Post the whole query, mate...
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 26, 2012 at 9:48 am
My bad! I hit another sp, it works, amazingly like what I expect. Thanks lots.
September 26, 2012 at 9:50 am
halifaxdal (9/26/2012)
My bad! I hit another sp, it works, amazingly like what I expect. Thanks lots.
Any time. There's a great set of articles by Paul White covering APPLY, links in my sig.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply