Geeks Talk

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.

distinct but not really distinct

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 ...

Go Back   Geeks Talk > Databases > SQL
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 06-18-2009
Junior Member
 
Join Date: Jun 2009
Location: GA
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
jkumar11 is on a distinguished road
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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-18-2009
Contributing Member
 
Join Date: Jun 2009
Location: United States
Posts: 71
Thanks: 0
Thanked 5 Times in 4 Posts
CSOOR is on a distinguished road
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.
Reply With Quote
  #3 (permalink)  
Old 06-18-2009
Junior Member
 
Join Date: Jun 2009
Location: GA
Posts: 7
Thanks: 2
Thanked 0 Times in 0 Posts
jkumar11 is on a distinguished road
Re: distinct but not really distinct

could you please give the SQL for this example? I am not sure how to use min() on col3. Do we have to use group or order by clause?

Thanks
Reply With Quote
  #4 (permalink)  
Old 06-18-2009
Contributing Member
 
Join Date: Jun 2009
Location: United States
Posts: 71
Thanks: 0
Thanked 5 Times in 4 Posts
CSOOR is on a distinguished road
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.
Reply With Quote
  #5 (permalink)  
Old 06-18-2009
Contributing Member
 
Join Date: Jun 2009
Location: United States
Posts: 71
Thanks: 0
Thanked 5 Times in 4 Posts
CSOOR is on a distinguished road
Re: distinct but not really distinct

in which server, are u working?
Reply With Quote
  #6 (permalink)  
Old 06-18-2009
Contributing Member
 
Join Date: Jun 2009
Location: United States
Posts: 71
Thanks: 0
Thanked 5 Times in 4 Posts
CSOOR is on a distinguished road
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
Reply With Quote
  #7 (permalink)  
Old 06-19-2009
Contributing Member
 
Join Date: Jun 2009
Location: United States
Posts: 71
Thanks: 0
Thanked 5 Times in 4 Posts
CSOOR is on a distinguished road
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 */
Reply With Quote
The Following User Says Thank You to CSOOR For This Useful Post:
  #8 (permalink)  
Old 06-22-2009
Expert Member
 
Join Date: Apr 2007
Location: Bangalore
Posts: 530
Thanks: 30
Thanked 63 Times in 61 Posts
susarlasireesha will become famous soon enough
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
Reply With Quote
The Following User Says Thank You to susarlasireesha For This Useful Post:
Reply

  Geeks Talk > Databases > SQL

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 10:14 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved