Oracle aggregate function to return a random value for a group?

Posted by tpdi on Stack Overflow See other posts from Stack Overflow or by tpdi
Published on 2010-06-18T01:32:58Z Indexed on 2010/06/18 1:43 UTC
Read the original article Hit count: 263

Filed under:
|
|
|

The standard SQL aggregate function max() will return the highest value in a group; min() will return the lowest.

Is there an aggregate function in Oracle to return a random value from a group? Or some technique to achieve this?

E.g., given the table foo:

group_id value
1        1
1        5
1        9
2        2
2        4
2        8

The SQL query

select group_id, max(value), min(value), some_aggregate_random_func(value)
from foo
group by group_id;

might produce:

group_id  max(value), min(value), some_aggregate_random_func(value)
1        9            1           1
2        8            2           4

with, obviously, the last column being any random value in that group.

© Stack Overflow or respective owner

Related posts about Oracle

Related posts about plsql