Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MSSQL Distinct, help

Posted on 2010-09-16
7
Medium Priority
?
537 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 143

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 2000 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

782 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