left join count - Mailing list pgsql-general

From Greenhorn
Subject left join count
Date
Msg-id ddcb1c341002111453v1b6e75c5i6c7979e57f82d5a2@mail.gmail.com
Whole thread Raw
Responses Re: left join count  (Richard Huxton <dev@archonet.com>)
List pgsql-general
Hi All,
I'm trying to retrieve the count of notes associated for each
transactions for table energy_transactions.
But I seem to be getting (after the join) the sum of amount x count of
notes.  Can someone enlighten me with this problem?
Here's the query that I am working with.

select
  energy_accounts_id, count(note)
  ,sum(case when t.fin_year = 2010 and t.fin_quarter = 1
      then t.total_amount else 0 end) as amount_current
  ,sum(case when t.fin_year = 2009 and t.fin_quarter = 1
      then t.total_amount else 0 end) as amount_last

from energy_transactions t
 left join energy_notes n on (t.id = n.energy_transactions_id)

group by energy_accounts_id, total_amount

Thanks in advance :)

pgsql-general by date:

Previous
From: Koichi Suzuki
Date:
Subject: Re: [HACKERS] Bug on pg_lesslog
Next
From: Richard Huxton
Date:
Subject: Re: left join count