Re: SQL question: Highest column value of unique column pairs

Lists: pgsql-adminpgsql-sql
From: Kevin Jenkins <gameprogrammer(at)rakkar(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: SQL question: Highest column value of unique column pairs
Date: 2008-01-12 00:10:19
Message-ID: 4788056B.2020809@rakkar.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-sql

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!


From: Shane Ambler <pgsql(at)Sheeky(dot)Biz>
To: Kevin Jenkins <gameprogrammer(at)rakkar(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL question: Highest column value of unique column pairs
Date: 2008-01-12 01:29:52
Message-ID: 47881810.7030303@Sheeky.Biz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-sql

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


From: Kevin Jenkins <gameprogrammer(at)rakkar(dot)org>
To:
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL question: Highest column value of unique column pairs
Date: 2008-01-12 03:06:20
Message-ID: 47882EAC.4030203@rakkar.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-sql

Thanks! How would I find the highest score in the union of the two tables?

I tried this but it can't find unionTable:

SELECT * FROM
(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) as unionTable
WHERE unionTable.score= (SELECT max(unionTable.score) FROM unionTable);

Shane Ambler wrote:
> 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
>
>
>
>
>


From: Shane Ambler <pgsql(at)Sheeky(dot)Biz>
To: Kevin Jenkins <gameprogrammer(at)rakkar(dot)org>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: SQL question: Highest column value of unique column pairs
Date: 2008-01-12 10:30:44
Message-ID: 478896D4.8090405@Sheeky.Biz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-sql

Kevin Jenkins wrote:
> Thanks! How would I find the highest score in the union of the two tables?
>
> I tried this but it can't find unionTable:
>
> SELECT * FROM
> (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) as unionTable
> WHERE unionTable.score= (SELECT max(unionTable.score) FROM unionTable);

the (select max(score)...) doesn't see the unionTable

change the last line to order by score desc limit 1

SELECT * FROM
(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) as unionTable

order by score desc limit 1

> Shane Ambler wrote:
>> 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
>>
>>
>>
>>
>>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: explain analyze is your friend
>

--

Shane Ambler
pgSQL (at) Sheeky (dot) Biz

Get Sheeky @ http://Sheeky.Biz


From: "Marc Mamin" <M(dot)Mamin(at)intershop(dot)de>
To: "Kevin Jenkins" <gameprogrammer(at)rakkar(dot)org>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: SQL question: Highest column value of unique column pairs
Date: 2008-01-14 08:39:16
Message-ID: CA896D7906BF224F8A6D74A1B7E54AB301750D2C@JENMAIL01.ad.intershop.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-sql


Hello Kevin,

I would use "select distinct on" to first isolate the candidates in (1)
and (2) and then reitere the query on this sub result:
(the query below will retrieve the last score, not the best one...)

something like (not tested):

select distinct on (date,name)
date,name,score
from

(select distinct (on date, LName1)
date,LName1 as name ,score1 as score
from table
order by date desc,LName1

union all

select distinct on (date, LName2)
date,LName2 as name,score2 as score
from table
order by date desc, LName2
)foo

order by date desc,name

regards,

Marc Mamin

-----Original Message-----
From: pgsql-sql-owner(at)postgresql(dot)org
[mailto:pgsql-sql-owner(at)postgresql(dot)org] On Behalf Of Kevin Jenkins
Sent: Saturday, January 12, 2008 1:10 AM
To: pgsql-sql(at)postgresql(dot)org
Subject: [SQL] SQL question: Highest column value of unique column pairs

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!

---------------------------(end of broadcast)---------------------------
TIP 7: You can help support the PostgreSQL project by donating at

http://www.postgresql.org/about/donate


From: PostgreSQL Admin <postgres(at)productivitymedia(dot)com>
To:
Cc: pgsql-admin(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Unclosed connections
Date: 2008-01-25 16:02:34
Message-ID: 479A081A.9020204@productivitymedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-sql

We are using this bad piece of the software that does not close
connections to the postgres server. Is there some setting for closing
dead connections? And not TCP/IP keep alive does not work.


From: "Scott Marlowe" <scott(dot)marlowe(at)gmail(dot)com>
To: "PostgreSQL Admin" <postgres(at)productivitymedia(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org, pgsql-sql(at)postgresql(dot)org
Subject: Re: [SQL] Unclosed connections
Date: 2008-01-25 16:19:09
Message-ID: dcc563d10801250819h2260eafbl8f56b65b979b6c7d@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-admin pgsql-sql

On Jan 25, 2008 10:02 AM, PostgreSQL Admin
<postgres(at)productivitymedia(dot)com> wrote:
> We are using this bad piece of the software that does not close
> connections to the postgres server. Is there some setting for closing
> dead connections? And not TCP/IP keep alive does not work.

If the TCP keepalive can't detect them as dead, how is something else
supposed to? I.e. if a keepalive packet gets answered, then the
connection isn't dead, something is still connected to it.