我的表有千行和两个主列(invoice_no
,exe_amount
),我想写下面的查询:
select invoice_ no,
exe_amount,
exe_amount*0.02 as edu_duty,
exe_amount*0.01 as ed_duty,
(exe_amount + edu_duty + ed_duty) as sub_total,
subtotal*0.0605 as st_amount,
sub_total*.0103 as ot_amount,
exe_amount + subtotal + st_amount + ot_amount as gd_amount
from table1;
如果我使用变量查询它会产生多个值的错误。
我该如何处理? Aany教程链接解决问题?
答案 0 :(得分:2)
您无法在计算时使用别名
select invoice_ no,
exe_amount,
exe_amount*0.02 as edu_duty,
exe_amount*0.01 as ed_duty,
(exe_amount + (exe_amount*0.02) + (exe_amount*0.01)) as sub_total,
((exe_amount + (exe_amount*0.02) + (exe_amount*0.01)))*0.0605 as st_amount,
(exe_amount + (exe_amount*0.02) + (exe_amount*0.01)) *.0103 as ot_amount,
( exe_amount +
(exe_amount + (exe_amount*0.02) + (exe_amount*0.01)) +
((exe_amount + (exe_amount*0.02) + (exe_amount*0.01)))*0.0605 + (exe_amount + (exe_amount*0.02) + (exe_amount*0.01)) *.0103
) as gd_amount
from table1;
Alter Native to above one is
SELECT invoice_ no,
exe_amount,
edu_duty ,
ed_duty,
sub_total,
subtotal*0.0605 as st_amount,
sub_total*.0103 as ot_amount,
exe_amount + subtotal + (subtotal*0.0605) + (sub_total*.0103) as gd_amount
FROM (
select invoice_ no,
exe_amount,
exe_amount*0.02 as edu_duty,
exe_amount*0.01 as ed_duty,
(exe_amount + (exe_amount*0.02) + (exe_amount*0.01)) as sub_total
from tableName
)t