Home > Blockchain >  How to sum where there's a condition from with different column
How to sum where there's a condition from with different column

Time:03-24

Here's the table

IP PID Aging
192.168.01 6891 3
192.168.01 6892 2
192.168.01 6893 1
192.168.01 6891 5

How to sum only row that has same IP and PID. (I want to sum aging column that only has the same IP and PID for example see the table that already bolded).

I already done several things using WHERE clause but there's no result that I wanted.

Thanks.

CodePudding user response:

If I understand correctly, you only need to aggregate by the IP and PID:

SELECT ID, PID, SUM(Aging) Aging
FROM yourTable
GROUP BY ID, PID;

CodePudding user response:

Worked in Oracle DB.

create table damith.temp_ip_list
    (
    IP varchar2(25),
    pid number ,
    aging number
    );
    
    insert into damith.temp_ip_list values ('192.168.01'    , 6891  , 3 );
    insert into damith.temp_ip_list values ('192.168.01'    , 6892  , 2 );
    insert into damith.temp_ip_list values ('192.168.01'    , 6893  , 1 );
    insert into damith.temp_ip_list values ('192.168.01'    , 6891  , 5 );
    
    commit;
    
    
    SELECT IP, PID, sum(AGING) as sum_aging FROM  damith.temp_ip_list group by IP, PID ;

**Output** 
   IP       PID   SUM_AGING
192.168.01  6891      8
192.168.01  6893      1
192.168.01  6892      2
  • Related