Question

The request is 2 part:

  1. Records should fall off report _*If the Record has expired AND if *_RecordStatus != 'Funded'.

  2. Records should stay on the report _If RecordStatus = 'Funded'*, has no *PURCHASEDATE, and if the Record has expired (is in the past from **currentdate).

I've tried to do this with CASE but this request appears to be boolean logic therefore, impractical. I've tried to looking into IF...THEN...ELSE but I'm lost as how to do it. I've spent 2 full days trying to get this to work and I've come up with nothing. Is there anyone out there that an assist me with this and then explain it?

Below is a simple example of failure (and yes, I full understand this fails due to a condition, not an expression):

Select distinct ...  
from ... --with joins  
where ...  
and case when RecordStatus != 'Funded' then getdate() <= EXPIRATIONDATE else ?? end

Thank you!!

Was it helpful?

Solution

The requirements seem suspect to me, but here is my view on it (no "conditional logic" needed) and why it seems "suspicious", if I'm reading it correctly ..

Records should stay on the report If RecordStatus = 'Funded', has no PURCHASEDATE, and if the Record has expired (is in the past from currentdate).

RecordStatus = 'Funded'
AND PurchaseDate IS NULL
AND ExpiredDate < CURRENT_TIMESTAMP

Records should fall off report If the Record has expired AND if RecordStatus != 'Funded'.

NOT (HasExpired AND RecordStatus <> 'Funded')

Combined:

RecordStatus = 'Funded'
AND PurchaseDate IS NULL
AND ExpiredDate < CURRENT_TIMESTAMP
AND NOT (HasExpired AND RecordStatus <> 'Funded')

With De Morgan's transformation:

RecordStatus = 'Funded'
AND PurchaseDate IS NULL
AND ExpiredDate < CURRENT_TIMESTAMP
AND (NOT HasExpired OR RecordStatus = 'Funded')

Elimination of subsumed condition after distribution.

RecordStatus = 'Funded'
AND PurchaseDate IS NULL
AND ExpiredDate < CURRENT_TIMESTAMP
AND NOT HasExpired

However, assuming that HasExpired is the same as ExpiredDate < CURRENT_TIMESTAMP

RecordStatus = 'Funded'
AND PurchaseDate IS NULL
AND ExpiredDate < CURRENT_TIMESTAMP
AND NOT ExpiredDate < CURRENT_TIMESTAMP

Which of course is never true and why I am suspect.

OTHER TIPS

You're current criteria a bit misleading.

You are saying in one case it should fall off the report and in the other case it should stay on, but you are referring to different status'.

I like to think of the things I want to see in a SQL query.

So I want to see the Funded status and I want to see PurchaseDate's of null.

SELECT ...
FROM ...
WHERE (RecordStatus = 'Funded' AND purchaseDate IS NULL) 

If you also wanted to see records of RecordStatus != 'Funded' but only if the record is current then add an OR

WHERE (RecordStatus = 'Funded' AND purchaseDate IS NULL) OR
      (RecordStatus != 'Funded' AND getDate() <= ExpirationDate)

Generally you build your logic in WHERE and rather avoid using CASE or IF it is not performance wise

...  
NOT (RecordStatus != 'Funded' AND getdate() > EXPIRATIONDATE) OR
(PURCHASEDATE IS NULL AND RecordStatus = 'Funded' AND getdate() > EXPIRATIONDATE)

You can optimize it further:

...
RecordStatus = 'Funded' OR getdate() <= EXPIRATIONDATE OR
PURCHASEDATE IS NULL
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top