Solved

MSSQL Distinct, help

Posted on 2010-09-16
7
510 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
[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
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 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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.
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

738 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