Solved

SQL - How to get distinct record based on a column

Posted on 2012-03-31
9
235 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
  • 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
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 37792345
try this then..SELECT DISTINCT ClientID, code FROM TableName ORDER BY ClientID ASC
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
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:
deepakChauhan 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

11 Experts available now in Live!

Get 1:1 Help Now