强制转换和求和函数-PostgreSQL
我试图找到铸造成本、货架价格和瓶装价格的总和,其中商品描述包括苏格兰威士忌或加拿大威士忌。但是,价格的值是一个价格,与select语句中的其他项目不同,它不是数字。起初我尝试了这个查询,它返回了一个错误,因为瓶子的价格需要以不同的方式进行转换强制转换和求和函数-PostgreSQL,sql,postgresql,casting,Sql,Postgresql,Casting,我试图找到铸造成本、货架价格和瓶装价格的总和,其中商品描述包括苏格兰威士忌或加拿大威士忌。但是,价格的值是一个价格,与select语句中的其他项目不同,它不是数字。起初我尝试了这个查询,它返回了一个错误,因为瓶子的价格需要以不同的方式进行转换 select SUM(case_cost)+SUM(shelf_price)+SUM(bottle_price) as total from public.products where item_description ilike '%Scotch%' o
select SUM(case_cost)+SUM(shelf_price)+SUM(bottle_price) as total
from public.products
where item_description ilike '%Scotch%' or item_description ilike '%Canad%';
之后,我创建了几个不同的查询并不断收到错误消息。这是我最近的一次尝试,只是简单地尝试让瓶子的铸造正确的价格,也许我包括了太多的括号,但我在“AS”数字部分得到了一个错误:
select (cast(sum (bottle_price AS numeric)))
from public.products
where item_description ilike '%Scotch%' or item_description ilike '%Canad%';
有人可以帮助解决这个强制转换问题吗?首先
cast
然后sum
生成的数值
select sum(cast(bottle_price AS numeric))
from public.products
where item_description ilike '%Scotch%' or item_description ilike '%Canad%';
这行吗
select (SUM(case_cost::numeric) + SUM(shelf_price::numeric) + SUM(bottle_price::numeric)
) as total
from public.products
where item_description ilike '%Scotch%' or item_description ilike '%Canad%';
我发现Postgres特有的
:
转换语法非常方便。另一方面,我觉得奇怪的是,Postgres不允许把钱加起来(默认情况下)。这取决于所需的结果类型
SUM(case_cost)+SUM(shelf_price)+SUM(bottle_price)::numeric as total
对于数值
结果或
SUM(case_cost)::money+SUM(shelf_price)::money+SUM(bottle_price) as total
对于
money
结果。瓶价的实际数据类型是什么?(PostgreSQL没有名为price的数据类型。)该列中的值是什么样的?