Solved

MSSQL Distinct, help

Posted on 2010-09-16
7
493 Views
Last Modified: 2012-05-10
Hey all,

I have an issue I have 2 tables with about 20,000 rows in. It contains name, email pins etc. Below see my SQL to retrieve all of the data.

What I need is a way of DISTINCTLY selecting the results but take the one with the highest X_ID.

SELECT x_id, name, email, primarypin
FROM cas_user_ext, cat_validation
WHERE cas_user_ext.x_id = cat_validation.id

This will return for example.

X_ID, name, email, primarypin
11, SChurch, SChurch@dsa.com, 115545544
12, SChurch, Schurch@dsa.com, 151516165

I would like to select the result with ID 12.
0
Comment
Question by:KazooSoft
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33690132
you might want to read this article:http://www.experts-exchange.com/A_3203.html
0
 
LVL 5

Expert Comment

by:Priya Perumpilavil
ID: 33690218
try this
SELECT max(x_id), max(name), max(email), max(primarypin)
FROM cas_user_ext, cat_validation
WHERE cas_user_ext.x_id = cat_validation.id
group by name
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 33690224
Try this
Raj
SELECT x_id, name, email, primarypin FROM
(
	SELECT x_id, name, email, primarypin
		, row_number() over (partition by [name], email order by X_ID desc) rowno 
	FROM cas_user_ext, cat_validation
	WHERE cas_user_ext.x_id = cat_validation.id
) A
WHERE A.ROWNO = 1

Open in new window

0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 33690276
Below...

HTH
SELECT distinct name, email, primarypin, max(x_id) as highest_x_id
FROM cas_user_ext, cat_validation
WHERE cas_user_ext.x_id = cat_validation.id
group by name, email, primarypin

Open in new window

0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 33690303
Sorry I misread the question..pls try...
select distinct
A.name, A.email, A.primarypin, A.x_id
from
(
select name, email, primarypin, x_id
FROM cas_user_ext, cat_validation
WHERE cas_user_ext.x_id = cat_validation.id
) A
inner join
(
SELECT distinct name, email, primarypin, max(x_id) as highest_x_id
FROM cas_user_ext, cat_validation
WHERE cas_user_ext.x_id = cat_validation.id
group by name, email, primarypin
) B
on A.x_id = B.highest_x_id

Open in new window

0
 
LVL 5

Expert Comment

by:Priya Perumpilavil
ID: 33690395
try this
SELECT max(x_id), name, max(email), max(primarypin)
FROM cas_user_ext, cat_validation
WHERE cas_user_ext.x_id = cat_validation.id
group by name

0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33690445
This is what you're looking for. I'm doing a self join to get the max record and then return the pertaining columns

SELECT a.x_id, a.name, a.email, a.primarypin
FROM cas_user_ext a, cas_user_ext b, cat_validation
WHERE b.x_id = cat_validation.id and max(b.x_id) = a.x_id
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

786 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