I have the below record set, which is created by joining two tables.
| A.ID | A.COMPANY_CODE | A.COUNTRY | B.CO_ID | B.CNTRY | COALESCE(B.CO_ID, B.CNTRY) | CNT |
|---|---|---|---|---|---|---|
| 1 | 1234 | United States of America | null | United States of America | United States of America | 33 |
| 1 | 1234 | United States of America | 1234 | null | 1234 | 6 |
| 1 | 1234 | United States of America | 1234 | null | 1234 | 1 |
| 2 | 5678 | United States of America | null | United States of America | United States of America | 33 |
I want to group by ID and get the sum(CNT) for each ID. In case B.CO_ID is present, it should only select the associated CNT and ignore CNTRY for that ID. And when CO_ID is null, I want to sum based on Country. Below is the desired output -
| A.ID | SUM(CNT) |
|---|---|
| 1 | 7 |
| 2 | 33 |
I tried writing a case condition but it is not working. Any guidance would be super helpful. Please let me know if I need to provide any additional details.