Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

distinct values from multiple fields within one table ORACLE SQL

How can I get distinct values from multiple fields within one table with just one request.

Option 1

SELECT WM_CONCAT(DISTINCT(FIELD1)) FIELD1S,WM_CONCAT(DISTINCT(FIELD2)) FIELD2S,..FIELD10S
  FROM TABLE;

WM_CONCAT is LIMITED

Option 2

select DISTINCT(FIELD1) FIELDVALUE, 'FIELD1' FIELDNAME
       FROM TABLE
UNION
select DISTINCT(FIELD2) FIELDVALUE, 'FIELD2' FIELDNAME
       FROM TABLE
... FIELD 10

is just too slow

like image 656
Rodriguez Avatar asked Jul 30 '26 05:07

Rodriguez


2 Answers

if you were scanning a small range in the data (not full scanning the whole table) you could use WITH to optimise your query e.g:

WITH a AS 
(SELECT field1,field2,field3..... FROM TABLE WHERE condition)
SELECT field1 FROM a
UNION   
SELECT field2 FROM a
UNION   
SELECT field3 FROM a
.....etc
like image 109
Kevin Burton Avatar answered Aug 01 '26 18:08

Kevin Burton


For my problem, I had

WL1   ...   WL2   ...  correlation
A            B             0.8
B            A             0.8
A            C             0.9
C            A             0.9

how to eliminate the symmetry from this table?

    select WL1, WL2,correlation from
    table
    where least(WL1,WL2)||greatest(WL1,WL2) = WL1||WL2
    order by WL1

this gives

WL1   ...   WL2   ...  correlation
A            B             0.8
A            C             0.9

:)

like image 30
Brainfreeze Avatar answered Aug 01 '26 20:08

Brainfreeze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!