我有一个名为REV的Impala表,其中包含每个电汇代码的wire_code,金额和报告行。
+---------+------+----------------+
|wire_code| amt | Reporting_line |
+---------+------+----------------+
| abc | 100 | Database |
+---------+------+----------------+
| abc | 10 | Revenue |
+---------+------+----------------+
| def | 50 | Database |
+---------+------+----------------+
| def | 25 | Polland |
+---------+------+----------------+
| ghi | 250 | Cost |
+---------+------+----------------+
| jkl | 300 | Cost |
+---------+------+----------------+
and the other table is FA which is having wire_code and Ajusted_wire_code
+---------+------+
|wire_code|adj_wc|
+---------+------+
| abc | def |
+---------+------+
| ghi | jkl |
+---------+------+
I need to adjust the amount of wire code which is available as adj_wc in FA table.
For example:
FA表中有“ abc”,并且将其调整为“ def”,然后我的输出应该是-wire_code“ def”的数量如下(abc和def),而“ abc”数量将保持不变。
我正在使用下面提供的查询,它正在删除两种电汇代码中都不常见的记录,例如,报告行Polland的def。并且abc有一个额外的报告行收入,当abc移至def时,需要将其添加到def电汇代码中。
abc正在调整为def-abc中不存在的def报告行将保持不变,并且将调整公共报告行。
select r.wire_code, r.amt+coalesce(a.amt,0) as amt
from REV r
left outer join FA f on r.wire_code=f.adj_wc --adjustments
left outer join REV a on f.wire_code=a.wire_code --adjusted amount
Where REP.REPORTING_LINE = REP1.REPORTING_LINE
;
预期结果:
+---------+------+----------------+
|wire_code| amt | Reporting_line |
+---------+------+----------------+
| abc | 100 | Database |
+---------+------+----------------+
| abc | 10 | Revenue |
+---------+------+----------------+
| def | 150 | Database |
+---------+------+----------------+
| def | 10 | Revenue |
+---------+------+----------------+
| def | 25 | Polland |
+---------+------+----------------+
| ghi | 250 | Cost |
+---------+------+----------------+
| jkl | 550 | Cost |
+---------+------+----------------+
答案 0 :(得分:0)
我认为下面的查询在蜂巢中工作
尝试黑斑羚,让我知道
create table rev
(
wire_code varchar(200),
amt int,
reporting varchar(200)
);
insert into rev values ('abc',100,'Database');
insert into rev values ('abc',10,'Revenue');
insert into rev values ('def',50,'Database');
insert into rev values ('def',25,'Polland');
insert into rev values ('ghi',250,'cost');
insert into rev values ('jkl',300,'cost');
create table fa
(
wire_code varchar(200),
adj_wc varchar(200)
);
insert into fa values ('abc','def');
insert into fa values ('ghi','jkl');
select rev.wire_code,
case when rev.wire_code=adj_wc then sum(amt) over(partition by reporting)
else amt end as amt,reporting
from rev inner join fa
on (rev.wire_code=fa.wire_code or rev.wire_code=fa.adj_wc)
order by 1