How to find the sum of value based on Adjustments in Impala query

允我心安 提交于 2019-12-25 03:28:18

问题


I have an Impala table named REV having wire_code, amount and Reporting line for each 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:

"abc" is there in FA table and it's getting adjusted to "def" then my output should be - wire_code "def" is having the amount of (abc and def) as below and "abc" amount will remain same.

I am using the query provided below and it is removing the records which are not common in both the Wire code, for example, def with reporting line, Polland. and abc has one extra reporting line Revenue which needs to be added to def wire code as abc is moving to def.

abc is getting adjusted to def - reporting lines of def which are not there in abc will remain same and the common reporting lines will be adjusted.

  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
    ;

Expected results:

+---------+------+----------------+
|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           |
+---------+------+----------------+

回答1:


I think below query working in hive

Try in impala and let me know

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


来源:https://stackoverflow.com/questions/55376463/how-to-find-the-sum-of-value-based-on-adjustments-in-impala-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!