delete duplicate payments without using a temporary table

CREATE TABLE [dbo].[payments](
      [paymentid] [int] IDENTITY(1,1) NOT NULL,
      [orderid] [int] NOT NULL,
      [dateentered] [datetime] NULL,
      [datebilled] [datetime] NULL,
      [type] [varchar](50) NOT NULL,
      [number] [varchar](80) NOT NULL,
      [expiration] [varchar](10) NULL,
      [CV2] [int] NOT NULL,
      [confirmation] [varchar](70) NOT NULL,
      [resultcode] [varchar](40) NOT NULL,
      [debit] [money] NOT NULL,
      [paymentinfo] [text] NULL,
      [ebayquantity] [varchar](25) NULL,
      [paymentfee] [varchar](30) NULL,
      [email] [varchar](300) NULL,
      [business] [varchar](200) NULL,
      [wait] [smallint] NULL,
 CONSTRAINT [PK_payments] PRIMARY KEY CLUSTERED
(
      [paymentid] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]


without using a temporary table,
first select all the payments where orderid, datebilled,type,number,resultcode,debit,paymentfee,email are equal
and then a command to delete all the duplicate payments
an payment.orderid may have 5 duplicate payments, I only want one
LVL 1
rgb192Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

devlab2012Commented:
delete from Payments where paymentid not in (select min(paymentid) from payments group by orderid, datebilled, type, number, resultcode, debit, paymentfee, email)
0
SharathData EngineerCommented:
try like this.
;WITH cte 
     AS (SELECT *, 
                ROW_NUMBER() 
                  OVER(PARTITION BY orderid,datebilled,TYPE,NUMBER,resultcode,debit,paymentfee,email ORDER BY orderid) rn
         FROM   payments) 
DELETE FROM cte 
WHERE       rn > 1

Open in new window

0
SharathData EngineerCommented:
or this.
;WITH cte 
     AS (SELECT *, 
                ROW_NUMBER() 
                  OVER(PARTITION BY orderid,datebilled,TYPE,NUMBER,resultcode,debit,paymentfee,email ORDER BY paymentid) rn
         FROM   payments) 
DELETE FROM cte 
WHERE       rn > 1

Open in new window

0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

rgb192Author Commented:
both queries return the same amount of rows

how can I be sure that I am not deleting all the payments
because I want to keep one payment
0
SharathData EngineerCommented:
If you can run the SELECT statement before delete, you can see the row number for duplicate records starting from 1 on wards. with filter as rn > 1, trying to delete all the duplciate records except the record with rn = 1.
;WITH cte 
     AS (SELECT *, 
                ROW_NUMBER() 
                  OVER(PARTITION BY orderid,datebilled,TYPE,NUMBER,resultcode,debit,paymentfee,email ORDER BY paymentid) rn
         FROM   payments) 
SELECT rn , *FROM cte 
ORDER BY orderid,datebilled,TYPE,NUMBER,resultcode,debit,paymentfee,email,paymentid

Open in new window

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
LowfatspreadCommented:
RN > 1 !
0
devlab2012Commented:
Does this query work or gives some error:

delete from Payments where paymentid not in (select min(paymentid) from payments group by orderid, datebilled, type, number, resultcode, debit, paymentfee, email)

The sub-query will select the first payment-id from each set of duplicate values. The outer delete query will delete the records for which payment-id is not in the selected list.
0
rgb192Author Commented:
Thanks
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 2005

From novice to tech pro — start learning today.