Home » SQL & PL/SQL » SQL & PL/SQL » Selecting the correct record from the table (Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production,TNS for IBM/AIX RISC System/6000: Version 11.2.0.1.0 - Production, Windows)
Selecting the correct record from the table [message #596842] |
Thu, 26 September 2013 12:36 |
|
gupta27
Messages: 31 Registered: August 2013 Location: Delhi
|
Member |
|
|
Hi ,
I have a table with following structure:
CREATE TABLE ID_comments
(
ID CHAR(10 BYTE) NOT NULL,
S_COMMENTS VARCHAR2(255 BYTE),
P_COMMENTS VARCHAR2(255 BYTE),
C_COMMENTS VARCHAR2(255 BYTE)
);
For each Id, I can have multiple records.
Below is the insert script of one of the ID:
Insert into ID_comments values ('0813654254','','JR/0813653606 single','');
Insert into ID_comments values ('0813654254','','JR/0813653606 single','');
Insert into ID_comments values ('0813654254','','JR/0813653606 SINGLE','');
Insert into ID_comments values ('0813654254','','JR','');
Insert into ID_comments values ('0813654254','Single 08136536066 x 6, 8pp','','');
Insert into ID_comments values ('0813654254','72 6pks short per Denise','','');
Insert into ID_comments values ('0813654290','','JR multiple','');
Insert into ID_comments values ('0813654290','','JR multiple','');
Insert into ID_comments values ('0813654290','','JR MULTIPLE','');
Insert into ID_comments values ('0813654290','','JR','');
Insert into ID_comments values ('0813654290','Multiple 08136536066 x 6, 8pp','','');
Insert into ID_comments values ('0813654290','72 6pks short','','ABC');
commit;
Now I want to select only one record from this table for an ID, which will have "not null" values for s_comments,p_comments,c_comments columns. If for some ID , there is no "not null" row for any column, then pick up the "null" row/value for that column.
Please let me know if more information is required.
|
|
|
|
|
Re: Selecting the correct record from the table [message #596846 is a reply to message #596842] |
Thu, 26 September 2013 13:01 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
SQL> with
2 data as (
3 select ID, S_COMMENTS, P_COMMENTS, C_COMMENTS,
4 row_number() over
5 (partition by id
6 order by nvl2(S_COMMENTS, 1, 0)+nvl2(P_COMMENTS, 1, 0)+nvl2(C_COMMENTS, 1, 0) desc)
7 rn
8 from ID_comments
9 )
10 select ID, S_COMMENTS, P_COMMENTS, C_COMMENTS
11 from data
12 where rn = 1
13 /
ID S_COMMENTS P_COMMENTS C_COMMENTS
---------- -------------------- -------------------- --------------------
0813654254 JR/0813653606 single
0813654290 72 6pks short ABC
2 rows selected.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Fri Apr 26 02:28:51 CDT 2024
|