dbTalk Databases Forums  

sql interogation

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss sql interogation in the comp.databases.postgresql.novice forum.



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

Default sql interogation - 10-25-2004 , 03:05 AM






I have a postgres table like this:

CREATE TABLE "temp50" (
"gc" character varying(36),
"co" character varying(7),
"data" date,
"ora" smallint

);

It contains the following records:

5003707G9G9419 22012BB 10-14-2004 82
5003707G9G9419 22012BC 10-14-2004 44
5003707G9G9419 22022BB 10-14-2004 82
5003707G9G9420 22022BC 10-18-2004 49

I'd like the result of the sql interogation to be like this:

5003707G9G9419 22012BB 10-14-2004 82
5003707G9G9420 22022BC 10-18-2004 49


Explanations:
I want like sql interogation to select only one record from each "gc" group
record (who has the same code "gc" (that means that the "gc" field will be
unique key)) with the following two condition:
1)from each "gc" group of records to select the record who has the value of
"ora" field maxim.
2)if two ore more record have the same value of the maxim value of the "ora"
to select oly one of them

Thanks!


---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster


Reply With Quote
  #2  
Old   
Steven Klassen
 
Posts: n/a

Default Re: sql interogation - 10-25-2004 , 09:00 AM






* cristivoinicaru <cristivoinicaru (AT) dmhi (DOT) ct.ro> [2004-10-25 10:05:22 +0200]:

Quote:
Explanations:

I want like sql interogation to select only one record from each
"gc" group record (who has the same code "gc" (that means that the
"gc" field will be unique key)) with the following two condition:

1)from each "gc" group of records to select the record who has the
value of "ora" field maxim.

2)if two ore more record have the same value of the maxim value of
the "ora" to select oly one of them
Try this out; this is from memory, so it hasn't been tested.

SELECT DISTINCT max(gc), co, data, ora FROM temp50 GROUP BY co, data, ora;

--
Steven Klassen - Lead Programmer
Command Prompt, Inc. - http://www.commandprompt.com/
PostgreSQL Replication & Support Services, (503) 667-4564

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings



Reply With Quote
  #3  
Old   
Tom Lane
 
Posts: n/a

Default Re: sql interogation - 10-25-2004 , 09:06 AM



"cristivoinicaru" <cristivoinicaru (AT) dmhi (DOT) ct.ro> writes:
Quote:
I want like sql interogation to select only one record from each "gc" group
record (who has the same code "gc" (that means that the "gc" field will be
unique key)) with the following two condition:
1)from each "gc" group of records to select the record who has the value of
"ora" field maxim.
2)if two ore more record have the same value of the maxim value of the "ora"
to select oly one of them
I don't think you can do (2) very nicely in standard SQL, because the
results aren't fully defined and the SQL standard does not like poorly
defined behaviors ;-). However there is a nonstandard extension in
Postgres that is exactly what you need: SELECT DISTINCT ON. See the
"weather reports" example in the SELECT reference page --- your "gc"
corresponds to weather stations and "ora" to report date.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org



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.