postgresql - 修复错误 "function array_length(bigint[]) does not exist"

标签 postgresql postgresql-9.1

我有一个递归查询,封装在一个函数中,它返回一个分层调查“结构”:

CREATE OR REPLACE FUNCTION get_survey_results(IN v_survey_id integer DEFAULT 1, IN v_survey_session_id integer DEFAULT NULL)
  RETURNS TABLE(sort_order bigint[], survey_question_id integer, question_text text, required_ind smallint, survey_session_id integer, custom_text text, option_value integer, survey_scale_id integer, survey_scale_type_id integer, parent_survey_question_id integer, parent_survey_scale_option_id integer) AS
$BODY$
BEGIN
    RETURN QUERY
        WITH RECURSIVE Survey_Structure (PATH, survey_question_id, question_text,
            required_ind, survey_session_id, custom_text,
            option_value, survey_scale_id, survey_scale_type_id,
            parent_survey_question_id, parent_survey_scale_option_id, CYCLE)
        AS (
            SELECT ARRAY[row_number() OVER (ORDER BY Survey_Question.sort_order)], Survey_Question.survey_question_id, Survey_Question.question_text, 
                Survey_Question.required_ind, Survey_Response.survey_session_id, Survey_Response.custom_text,
                Survey_Scale_Option.option_value, Survey_Question.survey_scale_id, Survey_Scale.survey_scale_type_id,
                0 as parent_survey_question_id, 0 AS parent_survey_scale_option_id, FALSE
            FROM ...etc...

我可以将 survey_id 传递给此函数 (SELECT * FROM get_survey_results(1)),它会返回该调查的所有问题和答案。

我想通过以下方式确定层次结构中节点的“级别”:

SELECT question_text, array_length(sort_order) AS level,
    ...etc...
FROM get_survey_results(1)

我的查询返回错误

function array_length(bigint[]) does not exist

PostgreSQL array documentation promise arrar_length() 将处理“任何数组”。

我做错了什么?是否有我需要安装的可选包?

最佳答案

仔细检查文档会发现 array_length() 采用两个 参数。

我只需要改变

SELECT question_text, array_length(sort_order) AS level, ...

SELECT question_text, array_length(sort_order, 1) AS 级别, ...

关于postgresql - 修复错误 "function array_length(bigint[]) does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21173086/

相关文章:

postgresql - 使用 Postgres 角色系统进行 Web 应用程序身份验证和角色管理是否合法?

java - 将行插入 Postgres 时出现空指针异常

postgresql - 什么 PostgreSQL 查询或 View 可以确定谁设置了用户权限?

curl - curl中的PostgreSQL查询

activerecord - Gentoo 上的 Redmine:#<Rails::Application::Configuration:0x00000000ea5538> 的未定义方法 `active_record' (NoMethodError)

python - PostgreSQL 和 psycopg2 : database does not exist

postgresql - Elixir UUID。 UUID不匹配时如何处理500错误

python - 尝试通过 SQLAlchemy 重新使用主键 ID 时出现问题

postgresql-9.1 - PostgreSQL 9.1 : ERROR: type "citext" does not exist

postgresql - Postgresql 9.1 中 array_agg 的限制