我试图在google中搜索我的sql语句有什么问题,但没有成功!
[Err] ERROR:运算符不存在:字符变化*字符变化 第16行:“数量”*“单价”为“总计”
提示:没有运算符匹配给定的名称和参数类型。您可能需要添加显式类型转换。
SELECT
"Trade Date",
"Due Date",
"Reference",
"Type",
"Symbol",
to_number("Quantity", '99999999D99')AS "Quantity",
to_number("Unit Price", '99999999D99')AS "Unit Price",
to_number("Amount", '99999999D99')AS "Amount",
to_number("Commission", '99999999D99')AS "Commission",
to_number("Charge", '99999999D99')AS "Charge",
to_number("VAT", '99999999D99')AS "VAT",
to_number("Net Amount", '99999999D99')AS "Net Amount",
"W/H",
to_number("Net Price", '99999999D99')AS "Net Price",
"Quantity" * "Unit Price" as "Total"
FROM
"public".aa
答案 0 :(得分:1)
您无法在定义它的同一级别使用列别名。因此,表达式"Quantity"
中对"Quantity" * "Unit Price"
的引用是指实际(varchar
)列,而不是您定义的别名。
如果要访问别名的值,则需要使用派生表:
SELECT "Trade Date",
"Due Date",
"Reference",
"Type",
"Symbol",
"Quantity",
"Unit Price",
"Amount",
"Commission",
"Charge",
"VAT",
"Net Amount",
"W/H",
"Net Price",
"Quantity" * "Unit Price" as "Total"
FROM (
SELECT "Trade Date",
"Due Date",
"Reference",
"Type",
"Symbol",
to_number("Quantity", '99999999D99') AS "Quantity",
to_number("Unit Price", '99999999D99') AS "Unit Price",
to_number("Amount", '99999999D99') AS "Amount",
to_number("Commission", '99999999D99') AS "Commission",
to_number("Charge", '99999999D99') AS "Charge",
to_number("VAT", '99999999D99') AS "VAT",
to_number("Net Amount", '99999999D99') AS "Net Amount",
"W/H",
to_number("Net Price", '99999999D99')AS "Net Price"
FROM "public".aa
) t
然而,更好的解决方案是不在varchar
列中存储数字。数字应存储在定义为numeric
或integer
varchar
或text
,从不的列中