Solved

MSSQL Distinct, help

Posted on 2010-09-16
7
476 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

759 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

20 Experts available now in Live!

Get 1:1 Help Now