Solved

SQL2005 Select Into - Remove Duplicates, Group By

Posted on 2009-05-08
7
418 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24338260
0
 
LVL 40

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 40

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 40

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

708 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

12 Experts available now in Live!

Get 1:1 Help Now