Solved

SQL - How to get distinct record based on a column

Posted on 2012-03-31
9
241 Views
Last Modified: 2012-04-04
How do I write a SQL statement to pick up the distinct record based on ClientID
For example, this is my dataset,
ClientID      code
1227      5200      
1234      5500      
1235      5100      
1235      5600      
1235      5700      
1236      5500      
1237      5200      
1238      5500    

I would like to get the result
1227      5200      
1234      5500      
1235      5100      
1236      5500      
1237      5200      
1238      5500
0
Comment
Question by:tommym121
[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
  • 2
  • 2
  • +3
9 Comments
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37792303
SELECT *
FROM TableName
WHERE clientid IN
(
   SELECT DISTINCT clientid
   FROM TableName
)
ORDER BY clientid
0
 
LVL 1

Expert Comment

by:bosscat
ID: 37792329
SELECT DISTINCT(ClientID), code FROM TableName ORDER BY ClientID ASC
0
 

Author Comment

by:tommym121
ID: 37792338
Try both does not get the result I want.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 37792345
try this then..SELECT DISTINCT ClientID, code FROM TableName ORDER BY ClientID ASC
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37792348
SELECT DISTINCT ON (clientid)   clientid, code FROM  tablename ORDER BY  clientid
0
 
LVL 1

Expert Comment

by:bosscat
ID: 37792379
Try this instead.

SELECT ClientID, MIN( code )
FROM tablename
GROUP BY ClientID
ORDER BY ClientID ASC
Capture2.PNG
0
 
LVL 10

Expert Comment

by:plummet
ID: 37792542
Where there are many rows for that ClientID, do you want to see the lowest Code?
0
 
LVL 15

Accepted Solution

by:
Deepak Chauhan earned 500 total points
ID: 37793845
Hi ,
   Use this hope so it will display the expected result that u want.

select clientid , code from (
select row_number() over( partition by clientid order by clientid
) as row,clientid , code
from tablename) x where row =1
0
 

Author Closing Comment

by:tommym121
ID: 37809189
Thanks
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

623 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