Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

sum column B with distinct column A?

763900Nov 25 2010 — edited Nov 26 2010
Hello,

Would anybody know if this is possible at all? Preferably within the function without further grouping or nesting as this is a much simpified explanation of an issue I am having with a far larger and more esoteric query...

If I have a table with ID codes in column A, which are often repeated over several rows, and USD amounts in column A.

eg.

ID¦USD
1¦1,000
2¦5,000
3¦2,000
3¦2,000
3¦2,000
3¦2,000
4¦5,000
4¦5,000
5¦20,000
5¦20,000
5¦20,000

I would like to sum the USD amount in column B, but only once per unique ID in column A, ie (1000+5000+2000+5000+20000 = 33000)

I tried the following syntax to no avail;
sum(USD) over(distinct ID) ob
I am assuming that the following would count each distinct USD amount, this is not what I am looking for;
sum(distinct USD) ob
FYI here is the query shell that I am trying to use this in, the problem I have is that the ID codes in the query results i've called 'table' duplicate (correctly) IDs on occassion, I only want my counts and sums to count each ID once though. I can easily count my distinct IDs but I am not sure how get it to sum the values in the other columns based on this.
select  geo_segment_name 
      , bus_grp_typ
      , agg_grp_sctr
      , sum(original_balance_usdm) ob                   --need this for distict class_ids
      , sum(current_balance_qport_usdm) cb         --need this for distict class_ids
      , count(distinct deal_id) deal_count
      , count(distinct class_id) class_count 
from (table)
group by geo_segment_name, bus_grp_typ, agg_grp_sctr
Could anyone please advise?

In your debt!

Thanks

Jon
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 24 2010
Added on Nov 25 2010
6 comments
14,159 views