简体   繁体   English

标量子查询产生多个元素

[英]Scalar subqueries produced more than one element

I know this error has been produced many time and a lot of answers came by but I believe every situation might be unique.我知道这个错误已经产生了很多次,并且得到了很多答案,但我相信每种情况都可能是独一无二的。

So i am trying to get a deficit value(imports - exports) from a table.所以我试图从表中获取赤字值(进口 - 出口)。 Both values are on one column两个值都在一列上

value account
100  export
200  import

SO now i need to calculate the deficit or surplus, which is either import-export or export-import.所以现在我需要计算赤字或盈余,即进出口或进出口。 I tried scalar subqueries but i am always getting this error.我尝试了标量子查询,但我总是收到此错误。

SELECT label, product_type,status,((select value from Task2.quarterly_report where account="Imports") - (select value from Task2.quarterly_report where account="Exports")) As trade_deficit

so basically i am trying to get a table with:-所以基本上我想找一张桌子: -

label  product_type status trade_deficit

Can anyone explain to me the issue and why is it happening and how to solve it.谁能向我解释这个问题,为什么会发生以及如何解决它。 Thanks in advance提前致谢

You can use the conditional aggregation:您可以使用条件聚合:

select sum(case when account = 'import' then value
                when account = 'export' then - value
           end)
from t;

This is based on the question and sample data.这是基于问题和样本数据。 I don't see what your query has to do with the rest of the question.我看不出您的查询与问题的 rest 有什么关系。

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

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