'How to sum where there's a condition from with different column
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.
Solution 1:[1]
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;
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Biegeleisen |
| Solution 2 | dami_prasa |
