Solved

SQL2005 Select Into - Remove Duplicates, Group By

Posted on 2009-05-08
7
426 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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
LVL 41

Accepted Solution

by:
Sharath earned 400 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 100 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

695 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