简体   繁体   English

标量子查询产生多个元素,使用 UNNEST

[英]Scalar subquery produced more than one element, using UNNEST

I have the following sentence, as I have read, the UNNEST should be used, but I don't know how我有以下句子,正如我所读的,应该使用UNNEST ,但我不知道如何

select
(
  select 
    case 
      when lin = 4 then 1
      when lin != 4 then null
    end as FLAG,
  from table_1
  WHERE
    table_1.date = table_2.date
  AND
    table_1.acc = table_2.acc
) as FLAG
FROM
  table_2

I have a lot of subqueries and that's why I can't use LEFT JOIN.我有很多子查询,这就是我不能使用 LEFT JOIN 的原因。 Currently my table_2 has 13 million records, and table_1 has 400 million records, what I want is to be able to show a FLAG for each account, knowing that the data universes are different.目前我的table_2有1300万条记录,table_1有4亿条记录,我想要的是能够为每个账户显示一个FLAG,知道数据宇宙是不同的。

I can't use LEFT JOIN ...我不能使用 LEFT JOIN ...
Scalar subquery produced more than one element ...标量子查询产生了多个元素...

simply use below version of your query - which logically is equivalent to your original query but eliminating the issue you have只需使用以下版本的查询 - 这在逻辑上等同于您的原始查询,但消除了您遇到的问题

select *, 
(
  select 1 as FLAG
  from table_1
  WHERE
    table_1.date = table_2.date
  AND
    table_1.acc = table_2.acc
  AND lin = 4
  LIMIT 1
) as FLAG
FROM
  table_2

This indicates that you expect to see one and only one value from the subquery but when you join to table_1 you are getting duplicates on the date and acc fields.这表明您希望从子查询中看到一个且只有一个值,但是当您加入table_1您会在dateacc字段上看到重复项。 If you aggregate the value or remove the duplicates from table_1 that should solve your issue, although at that point why not just use a more efficient JOIN ?如果您聚合值或从table_1中删除应该解决您的问题的重复项,那么为什么不使用更有效的JOIN呢?

-- This will solve your immediate problem, use any aggregation
-- technique, I picked MAX because why not. I do not know your use case.
select
(
  select 
    MAX(case 
      when lin = 4 then 1
      when lin != 4 then null
    end) as FLAG
  from table_1
  WHERE
    table_1.date = table_2.date
  AND
    table_1.acc = table_2.acc
) as FLAG
FROM
  table_2

A better way to do this would be一个更好的方法是

select
  case 
    when t1.lin = 4 then 1
    when t1.lin != 4 then null
  end as FLAG
FROM
  table_2 t2
LEFT JOIN
  table_1 t1 ON (
    t1.date = t2.date
    AND t1.acc = t2.acc
  )

As you said, left joins do not work for you.正如您所说,左连接对您不起作用。 If you want multiple results to be nested in the same subquery then just wrap your subquery in an ARRAY() function to allow for repeated values.如果您希望多个结果嵌套在同一个子查询中,那么只需将您的子查询包装在ARRAY()函数中以允许重复值。

select
ARRAY(
  select 
    case 
      when lin = 4 then 1
      when lin != 4 then null
    end as FLAG
  from table_1
  WHERE
    table_1.date = table_2.date
  AND
    table_1.acc = table_2.acc
  
  -- Now you must filter out results that will return
  -- NULL because NULL is not allowed in an ARRAY
  AND
    table_1.lin = 4

) as FLAG
FROM
  table_2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM