Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on distinct but not really distinct within the SQL forums, part of the Databases category; Hi, I have a problem in writing correct SQL. Could you please help me? I greatly appreciate your help. Table --------------------- Col1 |Col2| Col3 -------------------- 1 2 3 1 2 ...
|
|||||||
|
|||
|
distinct but not really distinct
Hi,
I have a problem in writing correct SQL. Could you please help me? I greatly appreciate your help. Table --------------------- Col1 |Col2| Col3 -------------------- 1 2 3 1 2 2 4 5 6 4 5 7 4 5 8 9 9 9 ------------------------- Result --------------------- Col1 |Col2| Col3 -------------------- 1 2 3 4 5 6 9 9 9 ---------------------- That is, if two or more rows are matching col1 & col2 then list only the first row. How do we write a sql for this? Thank you --JK |
| Sponsored Links |
|
|||
|
Re: distinct but not really distinct
use selfjoin to the table and select min value from third col3. Let me know if you did not get answer.
Last edited by CSOOR; 06-18-2009 at 04:28 PM. |
|
|||
|
Re: distinct but not really distinct
Try Group by . If you want to keep in the same order of your result then u can keep order by on col1 and col2
Last edited by CSOOR; 06-18-2009 at 06:49 PM. |
|
|||
|
Re: distinct but not really distinct
select a.col1,a.col2,min(a.col3)
from t1 a, t1 b where a.col1 = b.col1 and a.col2 = b.col2 group by a.col1,a.col2 order by a.col1,a.col2 try this and send me the result |
|
|||
|
Re: distinct but not really distinct
I think this will also work in simple way, no need to do like in the previous message.
select col1,col2,min(col3) from t1 group by col1,col2 order by col1,col2 and u will get the result 1 2 2 4 5 3 9 9 9 /* not 1 2 3, Do you want to go with the way you inserted ?? if yes then go with row-id */ |
| The Following User Says Thank You to CSOOR For This Useful Post: | ||
|
|||
|
Re: distinct but not really distinct
create table tab1(col1 number,col2 number,col3 number);
SET DEFINE OFF; Insert into TAB1 (COL1, COL2, COL3) Values (1, 2, 3); Insert into TAB1 (COL1, COL2, COL3) Values (1, 2, 2); Insert into TAB1 (COL1, COL2, COL3) Values (4, 5, 6); Insert into TAB1 (COL1, COL2, COL3) Values (4, 5, 7); Insert into TAB1 (COL1, COL2, COL3) Values (4, 5, 8); Insert into TAB1 (COL1, COL2, COL3) Values (9, 9, 9); COMMIT; select * from tab1 t where rowid = ( select min(rowid) from tab1 where col1 = t.col1 and col2=t.col2 ); Result -------------------- 1 2 3 4 5 6 9 9 9
__________________
Sireesha |
| The Following User Says Thank You to susarlasireesha For This Useful Post: | ||
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| difference bet unique and distinct | smitai | Oracle Certification | 4 | 10-28-2009 07:35 AM |
| Distinct Clause | vijay_zuzu | Oracle | 1 | 09-04-2008 10:35 PM |
| difference between unique and distinct | arpitace | Oracle | 2 | 08-12-2008 07:17 AM |
| Distinct keyword in Oracle SQL | ily_saravanan | SQL | 1 | 03-28-2008 11:45 PM |
| Load Distinct and Duplicate values to different targets | tanya123 | Data Warehousing | 1 | 02-25-2008 08:06 PM |