简体   繁体   English

从单个SQL表中删除除最新条目以外的所有条目

[英]Deleting all but the most recent entry from single SQL table

I have a single SQL table that contains multiple entries for each customerID (some customerID's only have one entry which I want to keep). 我有一个SQL表,其中每个客户ID包含多个条目(某些客户ID只有一个要保留的条目)。 I need to remove all but the most recent entry per customerID, using the invoiceDate field as my marker. 我需要使用invoiceDate字段作为标记,删除每个customerID以外的所有条目(最新条目除外)。

So I need to go from this: 所以我需要从这里开始:

+------------+-------------+-----------+
| customerID | invoiceDate | invoiceID |
+------------+-------------+-----------+
|          1 |  1393995600 |       xx  |
|          1 |  1373688000 |       xx  |
|          1 |  1365220800 |       xx  |
|          2 |  1265220800 |       xx  |
|          2 |  1173688000 |       xx  |
|          3 |  1325330800 |       xx  |
+------------+-------------+-----------+

To this: 对此:

+------------+-------------+-----------+
| customerID | invoiceDate | invoiceID |
+------------+-------------+-----------+
|          1 |  1393995600 |       xx  |
|          2 |  1265220800 |       xx  |
|          3 |  1325330800 |       xx  |
+------------+-------------+-----------+

Any guidance would be greatly appreciated! 任何指导将不胜感激!

  1. Write a query to select all the rows you want to delete: 编写查询以选择要删除的所有行:
SELECT * FROM t
WHERE invoiceDate NOT IN (
    SELECT MAX(invoiceDate)
    -- "FROM t AS t2" isn't supported by MySQL, see http://stackoverflow.com/a/14302701/227576
    FROM (SELECT * FROM t) AS t2
    WHERE t2.customerId = t.customerId
    GROUP BY t2.customerId
)

This may take a long time on a big database. 在大型数据库上,这可能需要很长时间。

  1. If you're satisfied, change the query to a DELETE statement: 如果您满意,请将查询更改为DELETE语句:
DELETE FROM t
WHERE invoiceDate NOT IN (
    SELECT MAX(invoiceDate)
    -- "FROM t AS t2" isn't supported by MySQL, see http://stackoverflow.com/a/14302701/227576
    FROM (SELECT * FROM t) AS t2
    WHERE t2.customerId = t.customerId
    GROUP BY t2.customerId
)

See http://sqlfiddle.com/#!9/6e031/1 看到http://sqlfiddle.com/#!9/6e031/1

If you have multiple rows whose date is the most recent for the same customer, you would have to look for duplicates and decide which one you want to keep yourself. 如果您有多行的日期是同一位客户最近的日期,则必须寻找重复的行并决定要保留哪一行。 For instance, look at customerId 2 on the SQL fiddle link above. 例如,在上面的SQL小提琴链接上查看customerId 2。

Let us asume that the table name is transaction_table . 让我们假设表名是transaction_table

create table test1 AS
select * from (
  select * from transaction_table order by customerID, invoiceDate desc) temp
group by customerID

You will have the output data in test1 table. 您将在test1表中获得输出数据。

Try out this one 试试这个

  with todelete as
(
            select 
            CustomerId, InvoiceId, InvoiceDate, Row_Number() over (partition by CustomerId  order by InvoiceDate desc) as Count
             from DeleteDuplicate
)


delete from todelete
where count > 1
delete from ex_4 where
rowid in
(select rowid
from ex_4 a 
where to_date(invoicedate,'DDMMYYYY') = (select max(to_date(invoicedate,'DDMMYYYY')) from ex_4 b where a.customerid != b.customerid))

This is how it will be done in oracle.This query will delete all but most recently added row.Looking at your table structure i am assuming that the invoicedate column is varchar2 type so converting it to date used to_date function here 这是在oracle中的处理方式。该查询将删除除最近添加的行以外的所有行。查看您的表结构,我假设invoicedate列为varchar2类型,因此将其转换为使用date_date函数的日期

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM