gpt4 book ai didi

sql - Cast 和 Sum 函数 - PostgreSQL

转载 作者:行者123 更新时间:2023-11-29 14:35:52 30 4
gpt4 key购买 nike

我试图找出 cast_cost、shelf_price 和 bottle_price 的总和,其中商品描述包括苏格兰威士忌或加拿大威士忌。但是 Bottle_price 的值是一个价格,而不是像 select 语句中的其他项目那样的数字。起初我尝试了这个查询,它返回了一个错误,因为 bottle_price 需要以不同的方式转换。

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%';

之后,我创建了几个不同的查询并不断收到错误消息。这是我最近的一次尝试,只是试图让 bottle_price 的转换正确,也许我包含了太多的括号,但我在“AS”数字部分遇到了错误:

select (cast(sum (bottle_price AS numeric)))
from public.products
where item_description ilike '%Scotch%' or item_description ilike '%Canad%';

任何人都可以帮助解决这个类型转换问题吗?

最佳答案

根据所需的结果类型,它可能是

SUM(case_cost)+SUM(shelf_price)+SUM(bottle_price)::numeric as total

对于 numeric 结果或

SUM(case_cost)::money+SUM(shelf_price)::money+SUM(bottle_price) as total

money 结果。

关于sql - Cast 和 Sum 函数 - PostgreSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44853960/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com