Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id EF001476A76 for ; Thu, 5 Sep 2002 11:55:50 -0400 (EDT) Received: from davinci.ethosmedia.com (davinci.ethosmedia.com [209.10.40.250]) by postgresql.org (Postfix) with ESMTP id 2FB25476A6C for ; Thu, 5 Sep 2002 11:55:50 -0400 (EDT) Received: from [216.135.165.74] (account ) by davinci.ethosmedia.com (CommuniGate Pro WebUser 3.5.9) with HTTP id 1634781; Thu, 05 Sep 2002 08:55:54 -0700 From: "Josh Berkus" Subject: Re: fumbling for join syntax To: Daniel Kelley , pgsql-sql@postgresql.org X-Mailer: CommuniGate Pro Web Mailer v.3.5.9 Date: Thu, 05 Sep 2002 08:55:54 -0700 Message-ID: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200209/25 X-Sequence-Number: 9302 Daniel, > select triv_a_r.login as user, count(triv_a_r.login) as score, > sum(triv_a_r.tm)/1000 as time > from triv_a_r LEFT OUTER JOIN triv_q_r ON (triv_a_r.tq_id = > triv_q_r.id) > where triv_a_r.ans = triv_q_r.ans > group by triv_a_r.login > order by score desc, time asc; A little SQL trick: select triv_a_r.login as user, SUM(CASE WHEN triv_a_r.ans = triv_q_r.ans THEN 1 ELSE 0 END) as score, sum(triv_a_r.tm)/1000 as time from triv_a_r LEFT OUTER JOIN triv_q_r ON (triv_a_r.tq_id = triv_q_r.id) group by triv_a_r.login order by score desc, time asc; Josh Berkus