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

x
?
Solved

SQL - How to get distinct record based on a column

Posted on 2012-03-31
9
Medium Priority
?
242 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

661 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