Home » SQL & PL/SQL » SQL & PL/SQL » Is there a way to select columns with largest column for each specific row (11.2.0.3)
Is there a way to select columns with largest column for each specific row [message #623840] |
Mon, 15 September 2014 05:54 |
kytemanaic
Messages: 55 Registered: February 2009
|
Member |
|
|
Hi,
here's my sql statement
conn / as sysdba
CREATE TABLESPACE test DATAFILE '+DATA_DG';
DROP TABLESPACE test INCLUDING CONTENTS AND DATAFILES;
CREATE USER test IDENTIFIED BY test DEFAULT TABLESPACE test TEMPORARY TABLESPACE temp QUOTA UNLIMITED ON test;
GRANT CONNECT, RESOURCE TO test;
conn test/test
--choose D
INSERT INTO max_data (cola, colb,colc, cold) VALUES (1,4,5,6);
--choose B
INSERT INTO max_data (cola, colb,colc, cold) VALUES (2,8,1,2);
--choose A
INSERT INTO max_data (cola, colb,colc, cold) VALUES (9,8,7,6);
--choose C
INSERT INTO max_data (cola, colb,colc, cold) VALUES (6,8,10,9);
SELECT * FROM max_data;
while it is easy to choose a row based on the maximum of colc, i.e.
TEST@ORCL1>SELECT rownum, a.* FROM (SELECT cola, colb,colc, cold FROM max_data ORDER BY colc desc)a WHERE rownum=1;
ROWNUM COLA COLB COLC COLD
---------- ---------- ---------- ---------- ----------
1 6 8 10 9
the requirement to choose the column name for each row based on the maximum values of all numerical columns for each specific row is a bit challenging, does any one has any idea of how to do it?
thanks a lot!
|
|
|
Re: Is there a way to select columns with largest column for each specific row [message #623842 is a reply to message #623840] |
Mon, 15 September 2014 06:00 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Quote:conn / as sysdba
NO! never for this.
Read SYS is special.
Quote:GRANT CONNECT, RESOURCE TO test;
No! Never use these roles.
Quote:the requirement to choose the column name for each row based on the maximum values of all numerical columns for each specific row ...?
What should be the result for the test case you gave?
Quote: is a bit challenging,
Not really.
Why are you selecting rownum if you want just one row?
Result of your test case:
SQL> INSERT INTO max_data (cola, colb,colc, cold) VALUES (1,4,5,6);
INSERT INTO max_data (cola, colb,colc, cold) VALUES (1,4,5,6)
*
ERROR at line 1:
ORA-00942: table or view does not exist
|
|
|
Re: Is there a way to select columns with largest column for each specific row [message #623884 is a reply to message #623842] |
Mon, 15 September 2014 20:29 |
kytemanaic
Messages: 55 Registered: February 2009
|
Member |
|
|
The result of the output for these data are as follow:
A B C D
1,4,5,6 => Choose D
2,8,1,2 => Choose B
9,8,7,6 => Choose A
6,8,10,9 => choose C
Quote: Why are you selecting rownum if you want just one row?
sorry for this sql, I confused the the users. It is actually to inform that it is not the answer I one.
the reason why the execution fails is because it left out the create statement for the table, sorry about that.
create table max_data (cola number, colb number,colc number, cold number);
thanks a lot!
|
|
|
Re: Is there a way to select columns with largest column for each specific row [message #623891 is a reply to message #623884] |
Tue, 16 September 2014 00:25 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
1/ What should be the result?
2/ And if you add (6,8,9,10) what should be the result?
3/ And (after adding the previous row) you remove your third one (6,8,10,9) what should be the result?
4/ Explain the rules with words
[Updated on: Tue, 16 September 2014 00:26] Report message to a moderator
|
|
|
Re: Is there a way to select columns with largest column for each specific row [message #623920 is a reply to message #623891] |
Tue, 16 September 2014 04:36 |
|
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
Are you perhaps looking for GREATEST?
SQL> col what_column format a10
SQL> With max_data As
2 ( Select 1 ident, 1 cola, 4 colb, 5 colc, 6 cold From dual Union all --D
3 Select 2 ident, 2 cola, 8 colb, 1 colc, 2 cold From dual Union all --B
4 Select 3 ident, 9 cola, 8 colb, 7 colc, 6 cold From dual Union all --A
5 Select 4 ident, 6 cola, 8 colb, 10 colc, 9 cold From dual --C
6 )
7 SELECT ident
8 , Case Greatest(cola, colb, colc, cold)
9 When cola Then 'COLA'
10 When colb Then 'COLB'
11 When colc Then 'COLC'
12 When cold Then 'COLD'
13 End what_column
14 , Greatest(cola, colb, colc, cold) what_value
15 FROM max_data
16 /
IDENT WHAT_COLUM WHAT_VALUE
---------- ---------- ----------
1 COLD 6
2 COLB 8
3 COLA 9
4 COLC 10
SQL>
MHE
|
|
|
Re: Is there a way to select columns with largest column for each specific row [message #623921 is a reply to message #623891] |
Tue, 16 September 2014 04:38 |
kytemanaic
Messages: 55 Registered: February 2009
|
Member |
|
|
if the data is stored as follows
A B C D
1,4,5,6
2,8,1,2
9,8,7,6
6,8,10,9
--result should be as follow
D
B
A
C
if I add 6,8,9,10
A B C D
1,4,5,6
2,8,1,2
9,8,7,6
6,8,10,9
6,8,9,10
--result should be as follow
D
B
A
C
D
if I add previous row and remove third row, then result would as follow
A B C D
1,4,5,6
2,8,1,2
9,8,7,6
6,8,9,10
results
D
B
A
D
Rules are as follow:
For each row, choose the column that has the highest numberical value, for example row 1
A B C D
1,4,5,6
choose D
for row 2
A B C D
2,8,1,2
choose B
|
|
|
|
|
|
Re: Is there a way to select columns with largest column for each specific row [message #623930 is a reply to message #623928] |
Tue, 16 September 2014 06:56 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Do you the value or do you want the column name?
Quote:A B C D
1,4,5,6
2,8,1,2
9,8,7,6
6,8,10,9
--result should be as follow
D
B
A
C
In the end, if you tell us what is your final goal maybe we can help in a more accurate and efficient way.
|
|
|
|
Re: Is there a way to select columns with largest column for each specific row [message #623964 is a reply to message #623927] |
Tue, 16 September 2014 20:04 |
kytemanaic
Messages: 55 Registered: February 2009
|
Member |
|
|
Quote:Do you the value or do you want the column name?
I want the column name
Quote:And in case of 2 columns have the maximum value which one do you want?
For instance for row (1,1,1,1) what should be the result?
in case of a tie the smallest column should be the result, i.e.
Quote:In the end, if you tell us what is your final goal maybe we can help in a more accurate and efficient way.
it is to find the earliest column that have the largest numerical value for every row.
e.g
A B C D
1,4,5,6
2,8,1,2
9,8,7,6
6,8,10,9
1,1,,1,1
result
D
B
A
C
A
thanks a lot!
|
|
|
|
|
|
Re: Is there a way to select columns with largest column for each specific row [message #623970 is a reply to message #623964] |
Wed, 17 September 2014 00:15 |
|
Michel Cadot
Messages: 68641 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
Then just use GREATEST (as Maartens showed) along with CASE or DECODE to display the column.
SQL> select d.*,
2 decode(greatest(cola,colb,colc,cold), cola,'A', colb,'B', colc,'C', cold,'D') res
3 from max_data d
4 /
COLA COLB COLC COLD R
---------- ---------- ---------- ---------- -
1 4 5 6 D
2 8 1 2 B
9 8 7 6 A
6 8 10 9 C
But I don't see any reason to use this, for our culture, can you explain your case.
|
|
|
Goto Forum:
Current Time: Fri Apr 19 07:58:30 CDT 2024
|