gp数据库---子查询报错 correlated subquery with skip-level correlations is not supported

gp数据库(greenPlum)代码jdbc执行sql报错信息:

SQL state [0A000]; error code [0]; [Pivotal][Greenplum JDBC Driver][Greenplum]correlated subquery with skip-level correlations is not supported. ; nested exception is java.sql.SQLException: [Pivotal][Greenplum JDBC Driver][Greenplum]correlated subquery with skip-level correlations is not supported.

问题现象:
要查询原sql在库里可以执行,但是代码jdbc执行会报错。
sql如下:

select
    (select
        CASE
            WHEN type = '12'
            THEN (select a.area_id from dim.dim_area_relation a where a.utrancell_district_name=district_name)
            WHEN type = '13'
            THEN (select a.parent_area_id from dim.dim_area_relation a where a.utrancell_district_name=district_name)
            ELSE '其他'
        END DISTRICT_ID
    from  dim.dim_area_relation a
    where a.utrancell_district_name = district_name) DISTRICT_ID
FROM
	ods.ods_sx_jl_conf_utrancell sjcu;

问题分析:
gp数据库---子查询报错 correlated subquery with skip-level correlations is not supported_第1张图片
上面的写法子查询里又包含的了一层子查询,sql我们可以拆分为3块,其中1和2都是子查询,3是主表查询。报错的原因是:在1的子查询里,用了3的字段名,属于跨级调用,代码中报错不支持这样的跨级调用。

修改过程如下:
去掉1的子查询

==============================================
示例,现将原sql和修改后的sql对比如下:
原sql:

select
    (select
        CASE
            WHEN type = '12'
            THEN (select a.area_id from dim.dim_area_relation a where a.utrancell_district_name=district_name)
            WHEN type = '13'
            THEN (select a.parent_area_id from dim.dim_area_relation a where a.utrancell_district_name=district_name)
            ELSE '其他'
        END DISTRICT_ID
    from  dim.dim_area_relation a
    where a.utrancell_district_name = district_name) DISTRICT_ID
FROM
	ods.ods_sx_jl_conf_utrancell sjcu;

修改后的sql:

select
    (select
        CASE
            WHEN type = '12' THEN a.area_id
            WHEN type = '13' THEN a.parent_area_id
            ELSE '其他'
        END DISTRICT_ID
    from  dim.dim_area_relation a
    where a.utrancell_district_name = district_name) DISTRICT_ID
FROM
	ods.ods_sx_jl_conf_utrancell sjcu;

修改后的sql,去掉了内部子查询,数据库和代码均可以执行。

你可能感兴趣的:(数据库)