?
Solved

SQL2005 Select Into - Remove Duplicates, Group By

Posted on 2009-05-08
7
Medium Priority
?
437 Views
Last Modified: 2012-05-06
I have this SQL 2005 table as shown with Duplicate Rows,

I wish to delete the duplicate rows, using Distinct CandidateID, Max(CreatedOn).
CandidateID should be the final Primary Key.

The Result Table should be the same as this SQL.
SELECT     CandidateID, MAX(CreatedOn) AS Expr2
FROM         db_Candidate
GROUP BY CandidateID


I think I should be changing from this code below, which does not give me Max(CreatedOn)
select * into a_Candidate
from (select  from db_Candidate) A

is this clear enough?

a.jpg
0
Comment
Question by:dennisdominic
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24338260
0
 
LVL 41

Expert Comment

by:Sharath
ID: 24338466

You already have the query. include INTO clause like this.
SELECT CandidateID, MAX(CreatedOn) AS CreatedOn
  INTO NewTable
  FROM db_Candidate
 GROUP BY CandidateID

Open in new window

0
 
LVL 1

Author Comment

by:dennisdominic
ID: 24338603
Hi Sharath 123
SELECT CandidateID, MAX(CreatedOn) AS CreatedOn INTO NewTable FROM db_Candidate GROUP BY CandidateID
This is very close, but can I include all the other columns as well?
0
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 
LVL 41

Accepted Solution

by:
Sharath earned 1600 total points
ID: 24338650
to include all other columns, you need this.
SELECT t1.*
  INTO NewTable
  FROM db_Candidate t1
  JOIN (SELECT CandidateID, MAX(CreatedOn) AS CreatedOn FROM db_Candidate GROUP BY CandidateID) t2
    ON t1.CandidateID = t2.CandidateID AND t1.CreatedOn = t2.CreatedOn

Open in new window

0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 400 total points
ID: 24338653
SELECT a.*
INTO NewTable
FROM db_Candidate a
INNER JOIN (

SELECT CandidateID, MAX(CreatedOn) AS CreatedOn
FROM db_Candidate
GROUP BY CandidateID
) b
On a.CandidateID = b.CandidateID AND A.CreatedOn =b.CreatedOn
0
 
LVL 41

Expert Comment

by:Sharath
ID: 24338664
or this
SELECT t1.*
  INTO NewTable
  FROM db_Candidate t1
 WHERE CreatedOn = (SELECT MAX(CreatedOn) FROM db_Candidate  t2 WHERE t1.CandidateID = t2.CandidateID)

Open in new window

0
 
LVL 1

Author Closing Comment

by:dennisdominic
ID: 31579549
both works but Sharath was faster.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

765 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