2014-02-27 33 views
0

想知道下面的查詢有什麼問題嗎?我的SQL Case Query有什麼問題

case 
b.transaction_id, 
when b.total_discount<0 then (ABS(b.total_discount)/(b.total_revenue + ABS(b.total_discount)) else '0' end as pct_discount_off 
from transaction b 

出現錯誤消息。

Msg 156, Level 15, State 1, Line 27 
Incorrect syntax near the keyword 'end' 
+1

缺少 ')'。 ABS(b.total_discount))) –

回答

1

你缺少一個closing bracket ')'

case 
b.transaction_id 
when b.total_discount<0 
then (ABS(b.total_discount)/(b.total_revenue + ABS(b.total_discount))) 
else '0' end as pct_discount_off 
0

我想你正在尋找的是這個

SELECT case 
when b.total_discount < 0 then (ABS(b.total_discount)/(b.total_revenue + ABS(b.total_discount))) 
else '0' end as pct_discount_off 
from [transaction] b 
0

使用

select case b.transaction_id when b.total_discount<0 then (ABS(b.total_discount)/(b.total_revenue + ABS(b.total_discount)) else '0' end as pct_discount_off from transaction b