Solved

SQL select without identical values

Posted on 2015-01-30
4
95 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:
Simon 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL STANDARD CORE 6 29
sql server query 12 24
IIF in access query 19 23
too many installs coming along with SQL 2016? 1 14
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

807 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