Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 7D5B82E0093 for ; Fri, 11 Jan 2008 21:30:06 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 59173-04 for ; Fri, 11 Jan 2008 21:29:59 -0400 (AST) Received: from ipmail01.adl6.internode.on.net (ipmail01.adl6.internode.on.net [203.16.214.146]) by postgresql.org (Postfix) with ESMTP id 837DE2E0092 for ; Fri, 11 Jan 2008 21:29:59 -0400 (AST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Aq4HACKnh0fLevdH/2dsb2JhbACBVqhG X-IronPort-AV: E=Sophos;i="4.24,274,1196602200"; d="scan'208";a="21245130" Received: from ppp247-71.static.internode.on.net (HELO DevBox.local) ([203.122.247.71]) by ipmail01.adl6.internode.on.net with ESMTP; 12 Jan 2008 11:59:55 +1030 Message-ID: <47881810.7030303@Sheeky.Biz> Date: Sat, 12 Jan 2008 11:59:52 +1030 From: Shane Ambler User-Agent: Thunderbird 2.0.0.6 (Macintosh/20070728) MIME-Version: 1.0 To: Kevin Jenkins CC: pgsql-sql@postgresql.org Subject: Re: SQL question: Highest column value of unique column pairs References: <4788056B.2020809@rakkar.org> In-Reply-To: <4788056B.2020809@rakkar.org> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200801/98 X-Sequence-Number: 29951 Kevin Jenkins wrote: > Hi, > > I have the following table which holds the result of 1 on 1 matches: > > FName1, LName1, Score1, FName2, LName2, Score2, Date > John, Doe, 85 Bill, Gates, 20 Jan 1. > John, Archer, 90 John, Doe, 120 Jan 5 > Bob, Barker, 70 Calvin, Klien 8 Jan 8 > John, Doe, 60 Bill, Gates, 25 Jan 3. > > So columns 1 and 2 hold the first person. Column 3 holds his score. > Columns 4 and 5 hold the second person. Column 6 holds his score. > > I want to return the most recent score for each person (be they an > opponent or myself). And the resultant table shouldn't care if they are > person 1 or 2. > > So the end result would be > > FName, LName, Score, Date > John, Doe, 120 Jan 5. > John, Archer 90 Jan 5. > Bob, Barker 70 Jan 8 > Bill, Gates 25 Jan 3 > Calvin Klien 8 Jan 8 > > Thanks for any help! > First I would say you should have one person in a row and have another table to join them like you want. Try (untested just guessing) - select fnam1 as fname,lname1 as lname, score1 as score from myscorestable union select fnam2 as fname,lname2 as lname, score2 as score from myscorestable order by 3 -- Shane Ambler pgSQL (at) Sheeky (dot) Biz Get Sheeky @ http://Sheeky.Biz