• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 111
  • Last Modified:

Case Statement Rewrite

select top (@IntTopRecords) m.unit, m.[Year], m.[Make], m.Model, SUM(case when r.rec > 0 then -r.rec else r.Labour + r.Parts + (r.cshop + r.cpart + r.clab) end) as TotalCost            
            from dbo.Repair r  


How would I rewrite above   sum(case    statement so

 if r.ca = 'R'  then I want to :
SUM(case when r.rec > 0 then -r.rec else r.Labour + r.Parts + (r.cshop + r.cpart + r.clab) end)

and if  r.ca <> 'R'  then I want to:      
SUM(case when r.rec > 0 then -r.rec else (r.cshop + r.cpart + r.clab) end)

as TotalCost
0
thayduck
Asked:
thayduck
1 Solution
 
Scott PletcherSenior DBACommented:
SUM(case when r.rec > 0 then -r.rec else (r.cshop + r.cpart + r.clab) + case when r.ca = 'R' then r.Labour + r.Parts else 0 end end)
0
 
thayduckProgrammer AnalystAuthor Commented:
Perfect, thanks...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now