在oracle中继承列值

时间:2015-07-06 08:15:34

标签: sql oracle

我有两个表TableA和TableB。两者都有相同的列。我想更新TableA的VALUE列值与TableB的VALUE列值。

这是我的表结构。

表A

C

表B

import outlook
mail = outlook.Outlook()
mail.login('emailaccount@live.com','yourpassword')
mail.inbox()
print mail.unread()

如果TableA的VALUE列没有任何值,那么我们必须使用TableB-> VALUE +最小日期值(100)更新TableA-> VALUE。这里TableA的最小日期值是100.所以我需要TableA中的值(100 + tableB-> VALUE)。

print mail.mailbody()
print mail.mailsubject()
print mail.mailfrom()
print mail.mailto()

根据条件继续列值。

请尽快给我查询。

感谢,

1 个答案:

答案 0 :(得分:0)

这可能是你所追求的:

with tablea as (select to_date('06/07/2015', 'dd/mm/yyyy') dt, 0 value from dual union all
                select to_date('05/07/2015', 'dd/mm/yyyy') dt, 0 value from dual union all
                select to_date('04/07/2015', 'dd/mm/yyyy') dt, 0 value from dual union all
                select to_date('03/07/2015', 'dd/mm/yyyy') dt, 0 value from dual union all
                select to_date('02/07/2015', 'dd/mm/yyyy') dt, 0 value from dual union all
                select to_date('01/07/2015', 'dd/mm/yyyy') dt, 100 value from dual),
     tableb as (select to_date('03/07/2015', 'dd/mm/yyyy') dt, 2 value from dual union all
                select to_date('01/07/2015', 'dd/mm/yyyy') dt, 3 value from dual)
select ta.dt,
       last_value(decode(ta.value, 0, null, ta.value) ignore nulls) over (order by ta.dt)
         + sum(tb.value) over (order by ta.dt) new_value
from   tablea ta
       left outer join tableb tb on (ta.dt = tb.dt)
order by ta.dt desc;

DT          NEW_VALUE
---------- ----------
06/07/2015        105
05/07/2015        105
04/07/2015        105
03/07/2015        105
02/07/2015        103
01/07/2015        103

但是,您没有说明在一系列零值之后A中存在非零值时会发生什么,很难说这是否会在这种情况下表现出您的预期。例如,具有相同的数据,但7月4日的tablea.value = 200:

with tablea as (select to_date('06/07/2015', 'dd/mm/yyyy') dt, 0 value from dual union all
                select to_date('05/07/2015', 'dd/mm/yyyy') dt, 0 value from dual union all
                select to_date('04/07/2015', 'dd/mm/yyyy') dt, 0 value from dual union all
                select to_date('03/07/2015', 'dd/mm/yyyy') dt, 200 value from dual union all
                select to_date('02/07/2015', 'dd/mm/yyyy') dt, 0 value from dual union all
                select to_date('01/07/2015', 'dd/mm/yyyy') dt, 100 value from dual),
     tableb as (select to_date('03/07/2015', 'dd/mm/yyyy') dt, 2 value from dual union all
                select to_date('01/07/2015', 'dd/mm/yyyy') dt, 3 value from dual)
select ta.dt,
       last_value(decode(ta.value, 0, null, ta.value) ignore nulls) over (order by ta.dt)
         + sum(tb.value) over (order by ta.dt) new_value
from   tablea ta
       left outer join tableb tb on (ta.dt = tb.dt)
order by ta.dt desc;

DT          NEW_VALUE
---------- ----------
06/07/2015        205
05/07/2015        205
04/07/2015        205
03/07/2015        105
02/07/2015        103
01/07/2015        103

如果我的查询没有完全按照您的要求进行操作,您将不得不更新您的问题以涵盖所有情况。您无法自行解决如何修改它。