Get list of all duplicate transactions

Good Day

Please assist in listing the duplcate transaction from the following table

I am having a table as follows

table Accounts
Columns
Accountno
LedgerEntry

Data is as follows
AccountNo            LedgerEntry
1                                    AA
1                                    AA
1                                    AA
2                                    BB
2                                    BB
3                                    CC
3                                    CC
3                                    CC
3                                    CC


Results Should be            

AccountNo            LedgerEntry
1                                    AA
1                                    AA
2                                    BB
3                                    CC
3                                    CC
3                                    CC

I need to remove always One entry if duplicates are found

For example if there is a 3 * duplicate for AccountNo 1 then two records should stay, because I need to create a list of all duplicates on accountno 1
except the original transaction
henryreynoldsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AnujSQL Server DBACommented:
for 2005 and above


CREATE TABLE #Account
(AccountNo INT,
 LedgerEntry CHAR(2)
 )
 
 INSERT INTO #Account
 SELECT 1, 'AA'
 UNION ALL
 SELECT 1, 'AA'
 UNION ALL
 SELECT 1, 'AA'
 UNION ALL
 SELECT 2, 'BB'
 UNION ALL
 SELECT 2, 'BB'
 UNION ALL
 SELECT 3, 'CC'
 UNION ALL
 SELECT 3, 'CC'
 UNION ALL
 SELECT 3, 'CC'
 UNION ALL
 SELECT 3, 'CC'


SELECT * FROM #Account

SELECT AccountNo, LedgerEntry
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY AccountNo, LedgerEntry ORDER BY AccountNo, LedgerEntry ) ID ,*
FROM #Account
)T
WHERE ID<> 1

DRO TABKE #Account
0
Rajkumar GsSoftware EngineerCommented:
;with cte as
(
select *, row_number() over( partition by AccountNo, LedgerEntry order by AccountNo, LedgerEntry) Serial
from table
)
-- select * from cte where Serial > 1
delete from cte  where Serial > 1

Open in new window

0
henryreynoldsAuthor Commented:
I am running sql server 2000
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ThomasianCommented:
Try this
SELECT A.*
FROM
	(SELECT DISTINCT Accountno, LedgerEntry FROM Accounts) A INNER JOIN
	(SELECT Accountno, LedgerEntry, total=COUNT(1)-1
	FROM Accounts 
	GROUP BY Accountno, LedgerEntry
	) T ON A.Accountno=T.Accountno AND A.LedgerEntry=T.LedgerEntry INNER JOIN
	master..spt_values n ON n.type='P' AND n.number BETWEEN 1 and total

Open in new window

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
for the note, you will find this article interesting in the context:
http://www.experts-exchange.com/A_3203.html
0
Rajkumar GsSoftware EngineerCommented:
Since Sql Server 2000 is not supporting Row_Number() and CTE, you can try another logic.

Logic:-
1. Create a temporary table with the same table structure of table 'Accounts'
2. Insert to this temp table, DISTINCT data from 'Accounts' table
3. Delete all records from 'Accounts' table
4. Insert into 'Accounts' table from 'Accounts_temp' table
5. Drop temp table

Please see the below posted script
-- check before you start - there may be duplicates
select * from Accounts


-- Step 1:- create temporary table
create table Accounts_Temp
(
	AccountNo    int,
	LedgerEntry	char(2)
)
go

-- Step 2:- Select from here for execution *************
BEGIN TRAN T1

-- fetch the distinct data (removing duplicates) into a temporary table
insert into Accounts_Temp select distinct * from Accounts
-- delete all data from original table
delete from Accounts
-- insert into original table from temp table (having no duplicates)
insert into Accounts select * from Accounts_Temp

COMMIT TRAN T1
-- Select until here for execution *************
---------------------------

-- drop temporary table
drop table Accounts_Temp

-- check now - there may be no duplicates
select * from Accounts

Open in new window


Alert - Take backup of your database before try this

Raj
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
henryreynoldsAuthor Commented:
thank you very much
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.