Link to home
Start Free TrialLog in
Avatar of BostonMA
BostonMAFlag for United States of America

asked on

SQL Case statement within a case statement syntax problem

Im wondering if someone can look at this sql statement and tell me where my syntax is incorrect.  

---This statement works

select

MonthTotal_Bucket1 = case when Date_ACTUALDATE = @PM_Comparison_StartDate then Amount1 else 0 end,
MonthTotal_Bucket1 = case when Date_ACTUALDATE >= @PM_Comparison_StartDate then Amount2 else 0 end


From Sales

blah blah


---This statement does not work.  The error message is: "Incorrect syntax near '='."

Select

case when @productflag =1 then

MonthTotal_Bucket1 = case when Date_ACTUALDATE = @PM_Comparison_StartDate then Amount1 else 0 end,

else

MonthTotal_Bucket1 = case when Date_ACTUALDATE > @PM_Comparison_StartDate then Amount1 else 0 end

end


From Sales

blah blah


Avatar of Nathan Riley
Nathan Riley
Flag of United States of America image

Like this?
Select 
 
case when @productflag =1 then
 
set MonthTotal_Bucket1 = case when Date_ACTUALDATE = @PM_Comparison_StartDate then Amount1 else 0 end,
 
else
 
set MonthTotal_Bucket1 = case when Date_ACTUALDATE > @PM_Comparison_StartDate then Amount1 else 0 end
 
end
 
 
From Sales

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BostonMA

ASKER

Thanks guys, but neither of those worked.

The first one by gallitin,  i got the message: "Incorrect syntax near the keyword 'set"

The second one by  matthew, isnt what I was aiming for.  I need  the MonthTotal_Bucket1 to be calculated differently depending on what the value of this flag is.  Let me write this isn psudeo code:

Select
Case when ProductFlag = 1 Then

MonthTotal_Bucket1 = case (criteria 1 current year date range)
MonthTotal_Bucket2 = case  (criteria 1 current prior year date range)
MonthTotal_Bucket3 = case (criteria 1 current prior year-1 date range)
MonthTotal_Bucket4 = case (criteria 1 current prior year-2 date range)
MonthTotal_Bucket5 = case (criteria 1 current prior year-3 date range)

Else
MonthTotal_Bucket1 = case (criteria 2 current year date range)
MonthTotal_Bucket2 = case  (criteria 2 current prior year date range)
MonthTotal_Bucket3 = case (criteria 2 current prior year-1 date range)
MonthTotal_Bucket4 = case (criteria 2 current prior year-2 date range)
MonthTotal_Bucket5 = case (criteria 2 current prior year-3 date range)
End


SO the output would look like the following everytime, except the values change in each column:

MonthTotal_Bucket1      MonthTotal_Bucket2      MonthTotal_Bucket3      MonthTotal_Bucket4      MonthTotal_Bucket5
100      48      4201      4524      1475
500      64      2451      1897      4517
75      487      748      1154      4156

Notice how the column name is the same, but the criteria used in calculating it changes based on the case statement...Does that make sense?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial