2014-02-27 37 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