如何将计算字段乘以货币字段?

时间:2018-11-14 01:54:27

标签: sql sql-server-2012

我尝试了所有我能想到的CAST,并且不会返回值的行是“百分比”值乘以P.pyd_amount(即Money字段)。

SELECT O.ord_hdrnumber, O.ord_totalweight, L.lgh_tot_weight, P.pyd_amount,  
CAST(O.ord_totalweight/L.lgh_tot_weight as decimal (10,2)) AS 'Percent', 
'Percent' * P.pyd_amount  as 'Breakdown' FROM Legheader L 
LEFT OUTER JOIN paydetail P ON (L.lgh_number = P.lgh_number) 
LEFT OUTER JOIN orderheader O ON (O.mov_number = L.mov_number) 
WHERE L.lgh_number = '2659'

1 个答案:

答案 0 :(得分:0)

Percent是别名列名称,不允许使用您在计算中使用的方式,您必须直接使用该表达式进行计算

select O.ord_hdrnumber, O.ord_totalweight, L.lgh_tot_weight, P.pyd_amount,  
Cast (O.ord_totalweight/L.lgh_tot_weight as decimal (10,2)) as 'Percent', 
Cast (O.ord_totalweight/L.lgh_tot_weight as decimal (10,2)) * P.pyd_amount  as 'Breakdown' from Legheader L 
Left outer join paydetail P on (L.lgh_number = P.lgh_number) 
left outer join orderheader O on (O.mov_number = L.mov_number) 
where L.lgh_number = '2659'