“查询结构与函数结果类型不匹配。返回类型双精度与第 1 列中的预期类型整数不匹配。”?
Posted
技术标签:
【中文标题】“查询结构与函数结果类型不匹配。返回类型双精度与第 1 列中的预期类型整数不匹配。”?【英文标题】:"structure of query does not match function result type. Returned type double precision does not match expected type integer in column 1."? 【发布时间】:2017-11-04 06:34:19 【问题描述】:错误:查询结构与函数结果类型不匹配详细信息: 返回的类型双精度与预期的整数类型不匹配 第 1 列. 上下文:PL/pgSQL 函数 get_analysis1_data(date,date) 返回查询的第 38 行 ********** 错误**********
postgresql
过程的代码:-
CREATE
OR REPLACE FUNCTION public.get_analysis1_data( IN date, IN date) RETURNS TABLE ( loc_no integer, loc_d_share double precision, loc_id_share double precision, loc_id_share_per double precision, loc_a integer, loc_m integer, loc_l integer, loc_oneway integer, loc_round integer, loc_replacement integer, loc_oncall integer, loc_avg_idshare double precision, out_no integer, out_d_share double precision, out_id_share double precision, out_id_share_per double precision, out_a integer, out_m integer, out_l integer, out_oneway integer, out_round integer, out_replacement integer, out_oncall integer, out_avg_idshare double precision ) AS $ BODY $
DECLARE in_from_date ALIAS FOR $1;
in_to_date ALIAS FOR $2;
loc_no integer;
loc_d_share double precision;
loc_id_share double precision;
loc_id_share_per double precision;
loc_a integer;
loc_m integer;
loc_l integer;
loc_oneway integer;
loc_round integer;
loc_replacement integer;
loc_oncall integer;
loc_avg_idshare double precision;
out_no integer;
out_d_share double precision;
out_id_share double precision;
out_id_share_per double precision;
out_a integer;
out_m integer;
out_l integer;
out_oneway integer;
out_round integer;
out_replacement integer;
out_oncall integer;
out_avg_idshare double precision;
BEGIN
RETURN Query
SELECT
( (
SELECT
count(*)
FROM
bookings
WHERE
(
is_outstation = FALSE
)
and
(
reporting_date BETWEEN in_from_date AND in_to_date
)
)
UNION
(
SELECT
coalesce(round(SUM(driver_share)), 0)
FROM
bookings
WHERE
is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
coalesce(round(SUM(id_share)), 0)
FROM
bookings
WHERE
is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
coalesce(round((SUM(id_share) / SUM(driver_share + id_share))*100), 0)
FROM
bookings
WHERE
is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
car_type = 'A'
AND is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
car_type = 'M'
AND is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
car_type = 'L'
AND is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
is_round_trip = FALSE
AND is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
is_round_trip = TRUE
AND is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
trip_type = 'Replacement'
AND is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
trip_type = 'OnCall'
AND is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
coalesce(round(AVG(id_share)), 0)
FROM
bookings
WHERE
is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
coalesce(round(SUM(driver_share)), 0)
FROM
bookings
WHERE
is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
coalesce(round(SUM(id_share)), 0)
FROM
bookings
WHERE
is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
coalesce(round(((SUM(id_share) / SUM(driver_share + id_share))*100)), 0)
FROM
bookings
WHERE
is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
car_type = 'A'
AND is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
car_type = 'M'
AND is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
car_type = 'L'
AND is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
is_round_trip = FALSE
AND is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
is_round_trip = TRUE
AND is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
trip_type = 'Replacement'
AND is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
count(*)
FROM
bookings
WHERE
trip_type = 'OnCall'
AND is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date)
UNION
(
SELECT
coalesce(round(AVG(id_share)), 0)
FROM
bookings
WHERE
is_outstation = TRUE
AND reporting_date BETWEEN in_from_date AND in_to_date) ) ;
END
$ BODY $ LANGUAGE plpgsql VOLATILE COST 100 ROWS 1000;
ALTER FUNCTION public.get_analysis1_data(date, date) OWNER TO
postgres;
【问题讨论】:
【参考方案1】:我认为您看到的错误将是众多错误中的第一个。您的函数表明它希望看到如下所示的记录集返回:
CREATE OR REPLACE FUNCTION public.get_analysis1_data(IN date, IN date)
RETURNS TABLE (
loc_no integer,
loc_d_share double precision,
loc_id_share double precision,
loc_id_share_per double precision,
loc_a integer,
loc_m integer,
loc_l integer,
loc_oneway integer,
loc_round integer,
loc_replacement integer,
loc_oncall integer,
loc_avg_idshare double precision,
out_no integer,
out_d_share double precision,
out_id_share double precision,
out_id_share_per double precision,
out_a integer,
out_m integer,
out_l integer,
out_oneway integer,
out_round integer,
out_replacement integer,
out_oncall integer,
out_avg_idshare double precision) AS
但据我所知,您的查询仅返回一列(要启动的混合数据类型)。其他 20 多个字段呢?
错误本身几乎无关紧要——您的查询返回双精度(可能是因为这个:coalesce(round((SUM(id_share) / SUM(driver_share + id_share)) * 100),
),但我认为这不是您真正的问题。我认为您的问题是您的查询看起来与函数所期望的完全不同。
如果我不得不猜测,您希望您的查询结果是水平的(旋转的)而不是垂直的——在一系列联合查询中。
我不推荐这个,但是你的函数的构建方式,看起来你想这样做:
SELECT count(*)
INTO loc_m
FROM bookings
WHERE car_type = 'A'
AND is_outstation = FALSE
AND reporting_date BETWEEN in_from_date AND in_to_date;
(注意添加了第二行,INTO
)
然后您的查询的最后一行将显示为:
return query select ... loc_m, ...
但我认为单个数据透视查询可能会更好。
【讨论】:
以上是关于“查询结构与函数结果类型不匹配。返回类型双精度与第 1 列中的预期类型整数不匹配。”?的主要内容,如果未能解决你的问题,请参考以下文章