无效子查询:标量子查询必须仅返回一列

问题描述 投票:0回答:1

当我在数据块中运行此查询时,出现以下错误

[INVALID_SUBQUERY_EXPRESSION.SCALAR_SUBQUERY_RETURN_MORE_THAN_ONE_OUTPUT_COLUMN] 无效子查询:标量子查询必须仅返回一列,但得到 3。 SQLSTATE: 42823

`select 
    all.a,
    all.b,
    all.c
from    com a11
    join    fam_grp a12
      on    ((a11.mkt_id || a11.brd_id) = (a12.mkt_id || a12.brd_id) and 
    a11.dataset = a12.dataset)
where   ((a11.cust_id,
    a11.mkt_id,
    a11.o_cust_id)
 in (((select   pc21.cust_id,
        pc21.mkt_id_1,
        pc21.o_cust_id
    from    ZZMD00  pc21
    group by    pc21.cust_id,
        pc21.mkt_id_1,
        pc21.o_cust_id)
 intersect (select  pc21.cust_id,
        pc21.mkt_id_1,
        pc21.o_cust_id
    from    ZZMD01  pc21
    group by    pc21.cust_id,
        pc21.mkt_id_1,
        pc21.o_cust_id)))
 and a11.dataset in ('C'))
group by    
    all.a,
    all.b,
    all.c`

我相信我在使用子查询的 where 子句之后遇到了问题,我猜这就是导致错误的原因。

sql mysql where-clause databricks
1个回答
1
投票

您无法在 MySQL 中使用行比较器,因此

IN
无法使用多列。

您需要将

EXISTS
INTERSECT
或内部
WHERE
过滤器一起使用。

此外,鉴于您使用的是

EXISTS
IN
,在子查询中使用
GROUP BY
无论如何都没有多大意义。

select 
    all.a,
    all.b,
    all.c
from com a11
join fam_grp a12
  on a11.mkt_id = a12.mkt_id
 and a11.brd_id = a12.brd_id
 and a11.dataset = a12.dataset
where exists (
    select
        a11.cust_id,
        a11.mkt_id,
        a11.o_cust_id

    intersect

    select
        pc21.cust_id,
        pc21.mkt_id_1,
        pc21.o_cust_id
    from ZZMD00 pc21

    intersect
    
    select
        pc21.cust_id,
        pc21.mkt_id_1,
        pc21.o_cust_id
    from ZZMD01 pc21
  )
  and a11.dataset in ('C')
group by    
    all.a,
    all.b,
    all.c;

或者

select 
    all.a,
    all.b,
    all.c
from com a11
join fam_grp a12
  on a11.mkt_id = a12.mkt_id
 and a11.brd_id = a12.brd_id
 and a11.dataset = a12.dataset
where exists (
    select
        pc21.cust_id,
        pc21.mkt_id_1,
        pc21.o_cust_id
    where a11.cust_id = pc21.cust_id
      and a11.mkt_id = pc21.mkt_id
      and a11.o_cust_id = pc21.o_cust_id
    from ZZMD00 pc21

    intersect
    
    select
        pc21.cust_id,
        pc21.mkt_id_1,
        pc21.o_cust_id
    from ZZMD01 pc21
  )
  and a11.dataset in ('C')
group by    
    all.a,
    all.b,
    all.c;

我不确定你为什么要连接

mkt_id
brd_id
,但它不太可能是正确的或高性能的。

© www.soinside.com 2019 - 2024. All rights reserved.