oracle's first_value function for postgres?

Started by Benover 20 years ago9 messagesgeneral
Jump to latest
#1Ben
bench@silentmedia.com

Oracle has a very handy function called first_value, which can be used
to turn a set like this:

a 10
a 3
b 1
c 30
c 10
d 1
d 20

...into this:

a 10
b 1
c 30
d 1

Does postgres have something equivalent, or, even better, is there a
reasonable way to express this in standard SQL?

#2Stephen Frost
sfrost@snowman.net
In reply to: Ben (#1)
Re: oracle's first_value function for postgres?

* Ben (bench@silentmedia.com) wrote:

Oracle has a very handy function called first_value, which can be used
to turn a set like this:

a 10
a 3
b 1
c 30
c 10
d 1
d 20

...into this:

a 10
b 1
c 30
d 1

Does postgres have something equivalent, or, even better, is there a
reasonable way to express this in standard SQL?

Isn't this just: distinct on (col1)?

Stephen

#3Martijn van Oosterhout
kleptog@svana.org
In reply to: Ben (#1)
Re: oracle's first_value function for postgres?

On Tue, Sep 13, 2005 at 12:15:21PM -0700, Ben wrote:

Oracle has a very handy function called first_value, which can be used
to turn a set like this:

Look at DISTINCT ON ()

Does postgres have something equivalent, or, even better, is there a
reasonable way to express this in standard SQL?

In standard SQL, not really, which is why it's an extention...
--
Martijn van Oosterhout <kleptog@svana.org> http://svana.org/kleptog/

Show quoted text

Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
tool for doing 5% of the work and then sitting around waiting for someone
else to do the other 95% so you can sue them.

#4Ben
bench@silentmedia.com
In reply to: Stephen Frost (#2)
Re: oracle's first_value function for postgres?

Sweet, indeed it is. DISTINCT ON seems to be a postgresism, but I can
live with that.

Thanks.

Stephen Frost wrote:

Show quoted text

Isn't this just: distinct on (col1)?

Stephen

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Martijn van Oosterhout (#3)
Re: oracle's first_value function for postgres?

Martijn van Oosterhout <kleptog@svana.org> writes:

On Tue, Sep 13, 2005 at 12:15:21PM -0700, Ben wrote:

Oracle has a very handy function called first_value, which can be used
to turn a set like this:

Look at DISTINCT ON ()

Does postgres have something equivalent, or, even better, is there a
reasonable way to express this in standard SQL?

In standard SQL, not really, which is why it's an extention...

Another way is to create an aggregate with a no-op transition
function ... also nonstandard, but you might be able to duplicate
Oracle's syntax that way.

regards, tom lane

#6Stephen Frost
sfrost@snowman.net
In reply to: Ben (#4)
Re: oracle's first_value function for postgres?

* Ben (bench@silentmedia.com) wrote:

Sweet, indeed it is. DISTINCT ON seems to be a postgresism, but I can
live with that.

Uhhh, I was pretty sure it was standard SQL... I'd have to go look it
up though, to be sure.

Don't forget to order by to make sure you get something consistent. :)

Enjoy,

Stephen

#7Tom Lane
tgl@sss.pgh.pa.us
In reply to: Stephen Frost (#6)
Re: oracle's first_value function for postgres?

Stephen Frost <sfrost@snowman.net> writes:

* Ben (bench@silentmedia.com) wrote:

Sweet, indeed it is. DISTINCT ON seems to be a postgresism, but I can
live with that.

Uhhh, I was pretty sure it was standard SQL...

Nope, definitely a Postgres-ism.

regards, tom lane

#8Stephen Frost
sfrost@snowman.net
In reply to: Tom Lane (#7)
Re: oracle's first_value function for postgres?

* Tom Lane (tgl@sss.pgh.pa.us) wrote:

Stephen Frost <sfrost@snowman.net> writes:

* Ben (bench@silentmedia.com) wrote:

Sweet, indeed it is. DISTINCT ON seems to be a postgresism, but I can
live with that.

Uhhh, I was pretty sure it was standard SQL...

Nope, definitely a Postgres-ism.

Huh. Guess I suck then. I actually do this a fair bit, is there any
way to do it in standard SQL? It's terribly useful...

Thanks,

Stephen

#9Ron Mayer
rm_pg@cheapcomplexdevices.com
In reply to: Martijn van Oosterhout (#3)
Re: oracle's first_value function for postgres?

Martijn van Oosterhout wrote:

On Tue, Sep 13, 2005 at 12:15:21PM -0700, Ben wrote:

Oracle has a very handy function called first_value, which can be used
to turn a set like this:

Look at DISTINCT ON ()

Does postgres have something equivalent, or, even better, is there a
reasonable way to express this in standard SQL?

In standard SQL, not really, which is why it's an extention...

No? It seems this is similar

SELECT a,b
FROM ( SELECT a,b,
RANK() OVER (
PARTITION BY a
ORDER BY b
) rank
FROM my_table )
WHERE rank = 1
ORDER BY a, rank DESC;

which I think is standard sql-99 with the sql-99 olap extention.