Solved

SQL2005 Select Into - Remove Duplicates, Group By

Posted on 2009-05-08
7
425 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding columns to a table which has non null columns 3 41
Remove () 10 39
When are cursors useful? 8 58
invoke-sqlcmd help 5 29
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

737 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