English 中文(简体)
消除在通道上的重复
原标题:Deleting duplicates in SQL in Access

I have a table that consists of the columns id(key), symbol, direction, range, price, percent. I would like to delete the data where the symbol, direction, price, and percent are the same that has the lowest range. How would I accomplish this?

I have been trying to alter this statement: DELETE FROM aw WHERE id not in (select min(id) from aw group by symbol, direction, price, percent); to get something to work.

最佳回答

此处为aw表格的样本数据。 根据您的描述,我认为你想抛弃“> 数值为2、3和5的行文”。

ID symbol direction range price percent
1  a      x            15    10       5
2  a      x            20    10       5
3  b      y            40    50       5
4  b      y            10    50       5
5  a      x            15    10       5

确定每个组合的最低范围值:symbol,direction,pricepercent

<>strong>qryMinRanges:

SELECT
    aw.symbol,
    aw.direction,
    aw.price,
    aw.[percent],
    Min(aw.range) AS MinOfrange
FROM aw
GROUP BY
    aw.symbol,
    aw.direction,
    aw.price,
    aw.[percent];

......结果如下:

symbol direction price percent MinOfrange
a      x            10       5         15
b      y            50       5         10

确定每个最低幅度的最低<代码>ID。

qryMinID_forMinRanges:

SELECT
    q.symbol,
    q.direction,
    q.price,
    q.[percent],
    q.MinOfrange,
    Min(aw.ID) AS MinOfID
FROM
    qryMinRanges AS q
    INNER JOIN aw
    ON
        (q.MinOfrange = aw.range)
        AND (q.[percent] = aw.[percent])
        AND (q.price = aw.price)
        AND (q.direction = aw.direction)
        AND (q.symbol = aw.symbol)
GROUP BY
    q.symbol,
    q.direction,
    q.price,
    q.[percent],
    q.MinOfrange;

......结果如下:

symbol direction price percent MinOfrange MinOfID
a      x            10       5         15       1
b      y            50       5         10       4

因此,qryMinID_forMinRanges 应当代表你希望保持的行文。 最终,您将从aw删除其发展价值未被列入qryMinID_forMinRanges的浏览量。 但是,首先尝试使用<条码>。 询问你是否重新确定删除的正确记录。

SELECT
    aw.ID,
    aw.symbol,
    aw.direction,
    aw.range,
    aw.price,
    aw.[percent]
FROM aw
WHERE aw.ID Not In
    (SELECT MinOfID FROM qryMinID_forMinRanges);

......这给我带来了这一结果:

ID symbol direction range price percent
 2 a      x            20    10       5
 3 b      y            40    50       5
 5 a      x            15    10       5

因此,如果看上去正确的话,它就改为DELETE。

DELETE *
FROM aw
WHERE
    (((aw.ID) Not In
        (SELECT MinOfID FROM qryMinID_forMinRanges)));

db发动机在DELETE之后需要*。 但是,如果你想利用问询设计师的预约,那么你需要给它一个实地观。

Please take precautions to safeguard your data in case I screwed up any details here. Backups are good. :-)

问题回答

使用<代码>exists 条款:

DELETE FROM aw 
WHERE 
    exists (
        select
            1
        from
            (select symbol, direction, price, percent, min(range) as minrange from aw
            group by symbol, direction, price, percent) aw2
        where
            aw2.symbol = aw.symbol
            and aw2.direction = aw.direction
            and aw2.price = aw.price
            and aw2.percent = aw.percent
            and aw2.minrange = aw.range
    )




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签