Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL select without identical values

Posted on 2015-01-30
4
Medium Priority
?
108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Simon earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

670 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