【问题标题】:Returned type bigint does not match expected type integer in column 3返回的类型 bigint 与第 3 列中的预期类型整数不匹配
【发布时间】:2019-04-24 20:24:22
【问题描述】:

下面是我的 sold_quantity 表结构(迁移文件)

alter table public.invoice_item add column sold_quantity int4 default 1;

下面是执行的函数

CREATE OR REPLACE FUNCTION sold_quantity()
RETURNS TABLE(
 invoiceid BIGINT,
 itemid BIGINT,
 sum_sold_quantity INT)
AS $$
BEGIN
 RETURN QUERY SELECT
 invoice_id as invoiceid, item_id as itemid, sum(sold_quantity) as
 sum_sold_quantity
 FROM
 invoice_item
 WHERE
 status='sold'
 GROUP BY
 invoice_id, item_id;
END; $$

我的代码有什么问题,请帮我解决这个错误

返回的 bigint 类型与第 3 列中的预期类型整数不匹配

【问题讨论】:

    标签: postgresql function types postgresql-9.5


    【解决方案1】:

    sum() 返回一个 bigint,不一定是要求和的列的类型。

    如果您 100% 确定您的总和不会超过整数范围,您可以在查询中使用强制转换来解决此问题:sum(sold_quantity)::int as sum_sold_quantity

    但最好调整一下函数的签名:

    CREATE OR REPLACE FUNCTION sold_quantity()
    RETURNS TABLE(
     invoiceid BIGINT,
     itemid BIGINT,
     sum_sold_quantity BIGINT)
    

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多