Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL aggregate function for any non-specific value from a group

Tags:

sql

oracle

Is there an agregate function that returns any value from a group. I could use MIN or MAX, but would rather avoid the overhead if possible given it's a text field.

My situation is an error log summary. The errors are grouped by the type of error and an example of the error text is displayed for each group. It doesn't matter which error message is used as the example.

SELECT
    ref_code,
    log_type,
    error_number,
    COUNT(*) AS count,
    MIN(data) AS example
FROM data
GROUP BY
    ref_code,
    log_type,
    error_number

What can I replace MIN(data) with to not have to compare 100,000s of varchar(2000) values?

like image 966
Hand-E-Food Avatar asked Jan 09 '13 00:01

Hand-E-Food


1 Answers

you can use MIN coupled with KEEP, like this:

MIN(data) keep (dense_rank first order by rowid) AS EXAMPLE

The idea behind this is that the database engine will be sorting data over ROWID instead of the VARCHAR(2000) values, which theoretically should be faster. You can replace ROWID with the primary key value, and check if it's faster

like image 78
cha Avatar answered Sep 20 '22 15:09

cha