Q:

Get distinct row by primary key, but use value from another column

I'm trying to get the sum of the total time that was spent sending all emails within a campaign. 
Because of the joins in my query I end up with the 'processing_time' column duplicated over many rows. So running sum(s.processing_time) as send_time will always over represent how long it took to run.
select
  c.id,
  c.sender,
  c.subject,
  count(*) as total_items,
  count(distinct s.id) as sends,
  sum(s.processing_time) as send_time,

from campaigns c
left join sends s on c.id = s.campaigns_id
left join opens o on s.id = o.sends_id

group by c.id;

I'd ideally like to do something like sum(s.processing_time when distinct s.id) but I can't quite work out how to achieve that.
I have made other attempts using case but I always run into the same issue, I need to get the distinct rows based on the ID column, but work with another column. 

A:

Since you want statistics related to distinct s.id as well as c.id, group by both columns. Collect the (intermediate) data that you need,
and use this table as the inner table in a nested sub-select query.
In the outer select, group by c.id alone.
Since the inner select groups by s.id, values which are unique per s.id will not get double-counted when you sum/group by c.id.
SELECT id
    , sender
    , subject
    , sum(total_items) as total_items
    , sum(sends) as sends
    , sum(processing_time) as send_time
FROM (
    SELECT
        c.id
        , s.id as sid
        , count(*) as total_items
        , 1 as sends
        , s.processing_time
        , c.sender
        , c.subject
    FROM campaigns c
    LEFT JOIN sends s on c.id = s.campaigns_id
    LEFT JOIN opens o on s.id = o.sends_id
    GROUP BY c.id, c.sender, c.subject, s.processing_time, s.id) t
GROUP BY id, sender, subject
ORDER BY id

Since the final table includes sender and subject, you'll need to group by these columns as well to avoid an error such as:
ERROR:  column "c.sender" must appear in the GROUP BY clause or be used in an aggregate function
LINE 14:         , c.sender

