MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 17th, 2007, 04:12 PM
jorge3921 jorge3921 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 4 jorge3921 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 16 sec
Reputation Power: 0
Select unique rows for all columns

Using DISTINCT with SELECT have effect only for one column.
But when is needed to select (or to count) queries for all rows for all columns in a table without duplicates, doesn't work.

Select DISTINCT a1,a2,a3,a4 From Y ---> results 167 rows
Select DISTINCT a4 From Y ---> " 85 rows

Any thoughts?


Jorge3921

Reply With Quote
  #2  
Old May 17th, 2007, 05:14 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
Quote:
Originally Posted by jorge3921
Using DISTINCT with SELECT have effect only for one column.
no, that's not true

DISTINCT applies to all columns in the SELECT

for example, consider this data --
Code:
A  B
1  37
2  25
3   9
4  37

select distinct A,B ... 4 rows

select distinct B ... 3 rows
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #3  
Old May 18th, 2007, 11:11 AM
jorge3921 jorge3921 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 4 jorge3921 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 16 sec
Reputation Power: 0
You are getting the wrong conclusion!!!!

If you have a table Y with 2 columns: A and B, and 4 rows, when doing

SELECT DISTINCT A,B from Y

you get 4 rows, including the one row with duplicates values on col B.
That is not a unique result for all the columns!
Meaning, to get the unique values the SELECT query must be done only for the colum with duplicates. But then the query output will be only to that column.
I need to query a big table with 15 columns and 6 million rows to get an output with all the columns, and all the rows with unique values in all the columns.
Maybe now you can understand what I mean.
Thanks.

Quote:
Originally Posted by r937
no, that's not true

DISTINCT applies to all columns in the SELECT

for example, consider this data --
Code:
A  B
1  37
2  25
3   9
4  37

select distinct A,B ... 4 rows

select distinct B ... 3 rows

Reply With Quote
  #4  
Old May 18th, 2007, 11:19 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
no, now i don't understand at all

perhaps give a more detailed example? how about an example with 5 columns?

in addition, you might also explain why you want what you want, to help us understand

Reply With Quote
  #5  
Old May 18th, 2007, 04:48 PM
wordracr wordracr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2002
Posts: 608 wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level)wordracr User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 4 Days 49 m 10 sec
Reputation Power: 14
maybe you mean this:
sql Code:
Original - sql Code
  1. SELECT *
  2. FROM y
  3. WHERE b IN (SELECT b FROM y GROUP BY b HAVING COUNT(*) = 1)


and btw,
Quote:
That is not a unique result for all the columns!
the result does contain the unique results for all of the columns selected when distinct was used. That is the functionality of distinct. You probably want what I've shown above in the sql example.

Reply With Quote
  #6  
Old May 21st, 2007, 01:45 PM
jorge3921 jorge3921 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 4 jorge3921 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 16 sec
Reputation Power: 0
Thank you for you participation wordracr. Your code will certainly output only all the rows with unique values for the selected column.
But what I need is to have all the rows with columns with unique data and one row for each of the rows with duplicate values for the selected cloumn.

Jorge3921

Reply With Quote
  #7  
Old May 21st, 2007, 01:51 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
Quote:
Originally Posted by jorge3921
... and one row for each of the rows with duplicate values for the selected cloumn.
huh?

which one is the selected column?

and if there is more than one row with the duplicated value for the selected column, then which row did you want?

you will really need to give more specific examples than what you have given for columns a1,a2,a3,a4

Reply With Quote
  #8  
Old May 21st, 2007, 03:19 PM
jorge3921 jorge3921 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2007
Posts: 4 jorge3921 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 50 m 16 sec
Reputation Power: 0
Thank you rc97 also, for answering.
Let me try to put it this way:
Using you simple example of an small table with two columns and four rows, there are only 3 rows with unique values for

A B
1 37
2 25
3 9
4 37

the column B.
Now, you wrote that :

select distinct A,B ... 4 rows

and as you can see DISTINCT don't eliminate the duplicated row with the value 37 under column B.
What I'm looking to resolve is to have a SELECT statement that will output all the rows with all the columns with unique values for column B.
In our example:

A B
1 37
2 25
3 9

The difference is that using DISTINCT,
as you wrote:

select distinct B ... 3 rows,

the output is only for column B.

B
37
25
9

As you can see, you wrote

select distinct B

meaning, only one selected column,because otherwise, if adding more columns to the select statement, will not detect the duplicates.

So, if there is a table with 1000 rows and 6 columns, and one of the columns is PHONE , and under Phone there are 100 rows with duplicates numbers, then there are only 950 rows with unique Phones numbers.
This is having 1 duplicates phones number per two rows.
What I need to get are the WHOLE 950 rows with all its columns in the output.
Which of the duplicates rows? Doesn't matter. But only one row per pairs of PHONE duplicated rows. Or one row per each trio of duplicated PHONE rows. Or one row per each quartet of duplicated PHONE rows, etc.

I hope that I was able to express it clearly now.

Thank you

Jorge3921.

Reply With Quote
  #9  
Old May 21st, 2007, 03:27 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
so you want something like this --
Code:
select min(col1)
     , max(col2)
     , avg(col3)
     , phone
  from daTable
group
    by phone
Comments on this post
f'lar agrees: LOL!

Reply With Quote
  #10  
Old July 23rd, 2009, 02:39 AM
UndiFineD UndiFineD is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 2 UndiFineD User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 39 m 48 sec
Reputation Power: 0
ok, what about a larger table with multiple duplicates floating around?
I have to fix a program that instead of using an optimised database chose to make it a big spreadsheet :-(
my efforts are succeeding into a new webbased program.

table:
aaaaa bbbbb ccccc ddddd eeeee
0001 alpha centauri rock 0.98
0002 beta centauri gas 1.43
0003 gamma centauri gas 3.03
0004 alpha epsilon lava 3.03
0005 alpha lyra rock 1.43

some silly example with a lot of duplicates

SELECT DISTINCT eeeee AS e FROM table WHERE eeeee >= 0.5 AND eeeee <= 1.5
SELECT DISTINCT ddddd AS d FROM table WHERE ddddd = 'rock'

so how do I JOIN these two tables without knowing which column has less resulting rows than the other ?

Reply With Quote
  #11  
Old July 23rd, 2009, 04:19 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
Quote:
Originally Posted by UndiFineD
SELECT DISTINCT eeeee AS e FROM table WHERE eeeee >= 0.5 AND eeeee <= 1.5
SELECT DISTINCT ddddd AS d FROM table WHERE ddddd = 'rock'

so how do I JOIN these two tables without knowing which column has less resulting rows than the other ?
which two tables? i see only one

Reply With Quote
  #12  
Old December 19th, 2012, 12:01 AM
humaira humaira is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 1 humaira User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 m 51 sec
Reputation Power: 0
SELECT *
FROM y
WHERE (b IN
(SELECT DISTINCT b
FROM y AS y_1))

Reply With Quote
  #13  
Old December 19th, 2012, 01:45 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
Quote:
Originally Posted by humaira
SELECT *
FROM y
WHERE (b IN
(SELECT DISTINCT b
FROM y AS y_1))
two comments about this

first, i doubt very much that UndiFineD is still waiting for a solution after three and a half years

second, the (il)logic of this solution is classic -- you can remove the WHERE clause for the same results

Reply With Quote
  #14  
Old December 21st, 2012, 02:11 PM
gk53 gk53 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 71 gk53 User rank is Sergeant (500 - 2000 Reputation Level)gk53 User rank is Sergeant (500 - 2000 Reputation Level)gk53 User rank is Sergeant (500 - 2000 Reputation Level)gk53 User rank is Sergeant (500 - 2000 Reputation Level)gk53 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 8 h 28 m 25 sec
Reputation Power: 8
If we came to original question

it looks like you looking for

Code:
select distinct min(a) as a, b 
from table1
group by b

Reply With Quote
  #15  
Old December 22nd, 2012, 08:56 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,376 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 7 h 35 m 43 sec
Reputation Power: 4140
Quote:
Originally Posted by gk53
Code:
select distinct min(a) as a, b 
from table1
group by b
in this query, DISTINCT is totally redundant and unnecessary

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Select unique rows for all columns

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap