Solved

SQL select without identical values

Posted on 2015-01-30
4
98 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 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

710 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