dbTalk Databases Forums  

Re: Subquery problems

comp.databases.postgresql comp.databases.postgresql


Discuss Re: Subquery problems in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Johan Nel
 
Posts: n/a

Default Re: Subquery problems - 10-27-2008 , 11:37 AM






Nico,

select fld3 from table t where t.fld2 in (select max(fld2) from table);

HTH,

Johan Nel
Pretoria, South Africa.

Nico wrote:
Quote:
Hi everybody, this is probably a lame question so I apologize in advance.

In a table like this:

Fld1, Fld2, Fld3

i launch a query like this:
SELECT MAX(Fld2) FROM table GROUP BY Fld2;
I would recover the value of Fld3 corresponding MAX(Fld2).

In msAccess I would have done a sort of Fld2 and then a First(Fld3), but
I know this is not valid in pgsql. Are there smarter commands (perhaps a
subquery) other than doing a join with the same table?

Thank you in advance.


Reply With Quote
  #2  
Old   
Johan Nel
 
Posts: n/a

Default Re: Subquery problems - 10-27-2008 , 11:37 AM






Nico,

select fld3 from table t where t.fld2 in (select max(fld2) from table);

HTH,

Johan Nel
Pretoria, South Africa.

Nico wrote:
Quote:
Hi everybody, this is probably a lame question so I apologize in advance.

In a table like this:

Fld1, Fld2, Fld3

i launch a query like this:
SELECT MAX(Fld2) FROM table GROUP BY Fld2;
I would recover the value of Fld3 corresponding MAX(Fld2).

In msAccess I would have done a sort of Fld2 and then a First(Fld3), but
I know this is not valid in pgsql. Are there smarter commands (perhaps a
subquery) other than doing a join with the same table?

Thank you in advance.


Reply With Quote
  #3  
Old   
Matthew Woodcraft
 
Posts: n/a

Default Re: Subquery problems - 10-27-2008 , 12:09 PM



Nico <n8 (AT) langhirano (DOT) it> wrote:
Quote:
In a table like this:

Fld1, Fld2, Fld3

i launch a query like this:
SELECT MAX(Fld2) FROM table GROUP BY Fld2;
I would recover the value of Fld3 corresponding MAX(Fld2).
Do you mean 'GROUP BY Fld1'?

Quote:
In msAccess I would have done a sort of Fld2 and then a First(Fld3), but
I know this is not valid in pgsql. Are there smarter commands (perhaps a
subquery) other than doing a join with the same table?
Joining with the same table is a sensible way.

Another option is to use DISTINCT ON, so something like

SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC;

But it's rather ugly. As usual, test with your own data to see how its
performance compares with other approaches.

-M-


Reply With Quote
  #4  
Old   
Matthew Woodcraft
 
Posts: n/a

Default Re: Subquery problems - 10-27-2008 , 12:09 PM



Nico <n8 (AT) langhirano (DOT) it> wrote:
Quote:
In a table like this:

Fld1, Fld2, Fld3

i launch a query like this:
SELECT MAX(Fld2) FROM table GROUP BY Fld2;
I would recover the value of Fld3 corresponding MAX(Fld2).
Do you mean 'GROUP BY Fld1'?

Quote:
In msAccess I would have done a sort of Fld2 and then a First(Fld3), but
I know this is not valid in pgsql. Are there smarter commands (perhaps a
subquery) other than doing a join with the same table?
Joining with the same table is a sensible way.

Another option is to use DISTINCT ON, so something like

SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC;

But it's rather ugly. As usual, test with your own data to see how its
performance compares with other approaches.

-M-


Reply With Quote
  #5  
Old   
Matthew Woodcraft
 
Posts: n/a

Default Re: Subquery problems - 10-27-2008 , 12:09 PM



Nico <n8 (AT) langhirano (DOT) it> wrote:
Quote:
In a table like this:

Fld1, Fld2, Fld3

i launch a query like this:
SELECT MAX(Fld2) FROM table GROUP BY Fld2;
I would recover the value of Fld3 corresponding MAX(Fld2).
Do you mean 'GROUP BY Fld1'?

Quote:
In msAccess I would have done a sort of Fld2 and then a First(Fld3), but
I know this is not valid in pgsql. Are there smarter commands (perhaps a
subquery) other than doing a join with the same table?
Joining with the same table is a sensible way.

Another option is to use DISTINCT ON, so something like

SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC;

But it's rather ugly. As usual, test with your own data to see how its
performance compares with other approaches.

-M-


Reply With Quote
  #6  
Old   
Matthew Woodcraft
 
Posts: n/a

Default Re: Subquery problems - 10-27-2008 , 12:09 PM



Nico <n8 (AT) langhirano (DOT) it> wrote:
Quote:
In a table like this:

Fld1, Fld2, Fld3

i launch a query like this:
SELECT MAX(Fld2) FROM table GROUP BY Fld2;
I would recover the value of Fld3 corresponding MAX(Fld2).
Do you mean 'GROUP BY Fld1'?

Quote:
In msAccess I would have done a sort of Fld2 and then a First(Fld3), but
I know this is not valid in pgsql. Are there smarter commands (perhaps a
subquery) other than doing a join with the same table?
Joining with the same table is a sensible way.

Another option is to use DISTINCT ON, so something like

SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC;

But it's rather ugly. As usual, test with your own data to see how its
performance compares with other approaches.

-M-


Reply With Quote
  #7  
Old   
Matthew Woodcraft
 
Posts: n/a

Default Re: Subquery problems - 10-27-2008 , 12:09 PM



Nico <n8 (AT) langhirano (DOT) it> wrote:
Quote:
In a table like this:

Fld1, Fld2, Fld3

i launch a query like this:
SELECT MAX(Fld2) FROM table GROUP BY Fld2;
I would recover the value of Fld3 corresponding MAX(Fld2).
Do you mean 'GROUP BY Fld1'?

Quote:
In msAccess I would have done a sort of Fld2 and then a First(Fld3), but
I know this is not valid in pgsql. Are there smarter commands (perhaps a
subquery) other than doing a join with the same table?
Joining with the same table is a sensible way.

Another option is to use DISTINCT ON, so something like

SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC;

But it's rather ugly. As usual, test with your own data to see how its
performance compares with other approaches.

-M-


Reply With Quote
  #8  
Old   
Coniglio Sgabbiato
 
Posts: n/a

Default Re: Subquery problems - 10-28-2008 , 02:21 AM



Quote:
Another option is to use DISTINCT ON, so something like

SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC;

But it's rather ugly. As usual, test with your own data to see how its
performance compares with other approaches.
I did not know this clause, good to know, it could be really useful.


Reply With Quote
  #9  
Old   
Coniglio Sgabbiato
 
Posts: n/a

Default Re: Subquery problems - 10-28-2008 , 02:21 AM



Quote:
Another option is to use DISTINCT ON, so something like

SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC;

But it's rather ugly. As usual, test with your own data to see how its
performance compares with other approaches.
I did not know this clause, good to know, it could be really useful.


Reply With Quote
  #10  
Old   
Coniglio Sgabbiato
 
Posts: n/a

Default Re: Subquery problems - 10-28-2008 , 02:21 AM



Quote:
Another option is to use DISTINCT ON, so something like

SELECT DISTINCT ON (Fld1) Fld3 FROM table ORDER BY Fld1, Fld2 DESC;

But it's rather ugly. As usual, test with your own data to see how its
performance compares with other approaches.
I did not know this clause, good to know, it could be really useful.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.