• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

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
0
rgb192
Asked:
rgb192
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
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
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now