Solved

SQL select without identical values

Posted on 2015-01-30
4
88 Views
Last Modified: 2015-01-30
Hello

I have a table "MyTable"  with the fields: MyTable.Id, MyTable.Value, MyTable.Text

How can i make a select which returns me all three fields, but not having the same MyTable.Value ?

"SELECT DISTINCT MyTable.Value FROM MyTable" returns only the column "MyTable.Value"
and
"SELECT MyTable.Value from MyTable group by MyTable.Value" also returns only the column "MyTable.Value"

But I need the MyTable.Text also in my selection.....

Regards

Eric

Ps: I have SQL 2008
0
Comment
Question by:ICSAutomation
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
SimonAdept earned 500 total points
ID: 40579402
Select min(mytable.id),mytable.value,min(mytable.text)
from mytable
group by mytable.value

Open in new window

or
select MT.* from Mytable MT inner join 
	(SELECT  value
	,row_number() over (partition by VALUE order by ID) as 'rn' 
	from practice
	) dt 
	on mt.value=dt.value
	where rdn=1

Open in new window


You're omitting all but one row for each value of MyTable.Value, so you can control WHICH of the rows is included in the result set by changing the ORDER BY clause of the row_number() function.
0
 

Author Closing Comment

by:ICSAutomation
ID: 40579429
good and fast!!!!
Thanks!
0
 
LVL 9

Expert Comment

by:rajeevnandanmishra
ID: 40579430
Hi Eric,

The requirement itself seems to elaborated a little more.
As per your data:
ID       Value        Text
1          10             ABC
2          10             XYZ

In this data you want all the three fields, but only one distinct value 10. Now you need to tell SQL Server that what you would like to have for [ID] and [Text] columns. Do you want Minimum value? Do you want Maximum value, or May be any other AGGREGATE value?

If you want Minimum value for [ID] and [Text] columns, then the query provided by Simon above is perfect.

But do consider, that minimum[ID] and minimum[Text] may not be picked from the same row. You could end up having [ID] from one row and [Text] from some other row.
0
 

Author Comment

by:ICSAutomation
ID: 40579510
Hello
Thanks for elaboration!!
Very good explanation!!

Eric
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now