left join count

From: Greenhorn <user(dot)postgresql(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: left join count
Date: 2010-02-11 22:53:52
Message-ID: ddcb1c341002111453v1b6e75c5i6c7979e57f82d5a2@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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 :)

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Richard Huxton 2010-02-11 23:28:30 Re: left join count
Previous Message Koichi Suzuki 2010-02-11 22:49:03 Re: [GENERAL] [HACKERS] Bug on pg_lesslog