简体   繁体   中英

Where clause with case statement and variable

Creating SSRS report with a parameter in which the user can select accounts with a balance greater than or equal to zero. Within my Select statement I have

, payment1 + payment 2 + payment3 + invoice AS CurrentBalance

And in my Where I have

and (case when @Balance = 1 then (payment1 + payment2 + payment3 + invoice) end > 0
   or
     case when @Balance = 0 then (payment1 + payment2 + payment3 + invoice) end = 0)

This kills my performance. Whats an alternative way to do this?

You could try this:

AND (
  (@Balance=1 AND payment1 + payment2 + payment3 + invoice > 0)
OR
  (@Balance=0 AND payment1 + payment2 + payment3 + invoice = 0)
)

This kills my performance. Whats an alternative way to do this? Maybe...

having (@Balance = 1 and currentBalance > 0) OR (@Balance = 0 and currentbalance = 0) 

I'm not sure if it has to re-do the math twice each time in the where clause which this may prevent..

Another alternative would be to add a computed column to the table so the payments1,2,3+invoice math is already done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM