使用select in update时oracle SQL中的问题

使用select in update时oracle SQL中的问题,sql,oracle,Sql,Oracle,所以,我有两个表,为了简单起见,我将一个显示两列,另一个只显示一列,但是它们有更多的数据 好的,这些表如下所示: _________________________________ | table 1 | +---------------------------------+ | gas_deu est_pag | +---------------------------------+ | 56857

所以,我有两个表,为了简单起见,我将一个显示两列,另一个只显示一列,但是它们有更多的数据

好的,这些表如下所示:

 _________________________________ 
|            table 1              |
+---------------------------------+
|    gas_deu         est_pag      |
+---------------------------------+
|     56857          (null)       |
|     60857          (null)       |
|     80857          (null)       |
+---------------------------------+

 ______________________
|      table 2         |
+----------------------+
|      gas_pag         |
+----------------------+
|       56857          |
|       21000          |
|       75857          |
+----------------------+
表1和表2可以使用id_edi和nr_dep(两个表中的名称相同)连接起来
这里发生的基本情况如下:
在表1中,我有gas_deu,这是一个属于某人的数字
在表2中,gas_pag是已支付的金额,这意味着gas_deu-gas_pag在全额支付gas_deu时应给出0(或负数),或者在部分支付gas_deu时给出正数
此外,表1中的一些行不在表2中,这意味着天然气单位根本没有支付。

我需要做的是用以下内容更新表1中的est_pag:
如果gas_deu-gas_pag0(部分支付),则将est_pag值更新为2
如果一行在表1中,但不在表2中,则该行尚未支付,因此est_pag值将为3

我已经尝试了很多,我的意思是更新中有很多代码,表中有很多列,所以我不会发布我尝试过的代码,因为这只会让人更加困惑
我主要尝试在更新集和更新位置中使用select子查询。它们总是给我一个单行错误,我知道会发生这种情况,因为它返回多个值,但是我该怎么做呢?我看不到只获取查询的一个值并只更新与债务状态匹配的行的方法。


对我来说,使用case是合乎逻辑的选择,但它似乎总是返回多行(尝试创建一个case(当gas_deu-gas_pag可以通过左外部联接进行更新时)。第二个表可能对同一个帐户有多个付款(客户可能进行部分付款),因此我首先按ID聚合第二个表。您没有提供用于测试的输入数据,因此在我的小测试中,我假设表由
ID
匹配(第一个表中的主键,第二个表中的外键-即使我没有将约束写入表定义)

按照我编写更新的方式,如果第二个表中不存在帐户,而且如果帐户存在,但
gas\u pag
列中存在
NULL
,则将分配3。(最好从一开始就声明该列为
非NULL
)如果在这种情况下需要不同的处理方式,您没有说;但是可以很容易地适应

好了,开始吧

表创建

create table t1 ( id number, gas_deu number, est_pag number );

insert into t1
  select 101, 56857, null from dual union all
  select 104, 60857, null from dual union all
  select 108, 80857, null from dual
;

create table t2 ( id number, gas_pag number ) ;

insert into t2
  select 101, 56857 from dual union all
  select 104, 60000 from dual
;

select * from t1;

        ID    GAS_DEU    EST_PAG
---------- ---------- ----------
       101      56857           
       104      60857           
       108      80857           

select * from t2;

        ID    GAS_PAG
---------- ----------
       101      56857
       104      60000
更新语句

update 
  ( select est_pag, gas_deu, gas_pag 
    from   t1 left join 
           ( select id, sum(gas_pag) as gas_pag from t2 group by id ) x
             on t1.id = x.id
  )
set    est_pag = case when gas_deu <= gas_pag then 1
                      when gas_deu >  gas_pag then 2
                      else                             3
       end
;

select * from t1;

        ID    GAS_DEU    EST_PAG
---------- ---------- ----------
       101      56857          1
       104      60857          2
       108      80857          3

你的代码在哪里?表结构在哪里?你的示例输入和输出在哪里?我没有发布代码,因为在这一点上它是一个巨大的mes,而且列名是巨大的,我提到了表结构,两个表都可以由id_edi和nr_dep连接,它们在两个表中都有相同的名称。我将添加一个表,显示我将得到什么。不能吗告诉我所付债务和部分支付债务之间的差异。如果我欠100英镑(汽油税)和50英镑(汽油税)然后我只付了一部分,因为我还欠着钱,也就是说,当我只付了一部分债务时,我只付了一部分。根据你给出的逻辑编辑一些代码添加到问题库中,它永远不会执行第二行,因为它们的条件相同。
如果gas_deu-gas_pag
create table t1 ( id number, gas_deu number, est_pag number );

insert into t1
  select 101, 56857, null from dual union all
  select 104, 60857, null from dual union all
  select 108, 80857, null from dual
;

create table t2 ( id number, gas_pag number ) ;

insert into t2
  select 101, 56857 from dual union all
  select 104, 60000 from dual
;

select * from t1;

        ID    GAS_DEU    EST_PAG
---------- ---------- ----------
       101      56857           
       104      60857           
       108      80857           

select * from t2;

        ID    GAS_PAG
---------- ----------
       101      56857
       104      60000
update 
  ( select est_pag, gas_deu, gas_pag 
    from   t1 left join 
           ( select id, sum(gas_pag) as gas_pag from t2 group by id ) x
             on t1.id = x.id
  )
set    est_pag = case when gas_deu <= gas_pag then 1
                      when gas_deu >  gas_pag then 2
                      else                             3
       end
;

select * from t1;

        ID    GAS_DEU    EST_PAG
---------- ---------- ----------
       101      56857          1
       104      60857          2
       108      80857          3
drop table t1 purge;
drop table t2 purge;