0

目标:
显示重复数据,在表格​​测试中,应删除。
但是,如果有两个重复数据,例如“3412 New York”。测试表中应仅显示 1 行。

问题:
不知道怎么做。请记住,此表可以是一百万行。

All data from the list

table testing
Region          Category    Energy
----------------------------------
1235 Delaware   candy       5
1235 Delaware   candy       5
1235 Delaware   candy       5
1235 Delaware   candy       5
1235 Delaware   candy       5
1235 Delaware   candy       5
1235 Delaware   candy       5
3125 New Jersey drink       4
3125 New Jersey drink       4
3125 New Jersey drink       4
3125 New Jersey drink       4
3125 New Jersey drink       4
3412 New York   food        3
3412 New York   food        3
3412 Washington coke        7
3412 california chips       20
3412 california chips       20
3412 california chips       20
3412 california chips       20





Requested result that should display data in table testing
Table testing
Region          Category    Energy
----------------------------------
1235 Delaware   candy       5
1235 Delaware   candy       5
1235 Delaware   candy       5
1235 Delaware   candy       5
1235 Delaware   candy       5
1235 Delaware   candy       5
3125 New Jersey drink       4
3125 New Jersey drink       4
3125 New Jersey drink       4
3125 New Jersey drink       4
3412 New York   food        3
3412 california chips       20
3412 california chips       20
3412 california chips       20
4

2 回答 2

1

查看结果

SELECT
    Region, Category, Energy
FROM
    (
    SELECT
       Region, Category, Energy,
       ROW_NUMBER() OVER (PARTITION BY Region, Category, Energy ORDER BY Region) AS rn
    FROm
       MyTable
    ) X
WHERE
   X.rn > 1
于 2012-07-24T17:53:08.887 回答
0

因为您没有主键,所以您可以使用 group by 或 distinct 重新生成表:

select distinct Region, Category, Energy
into newt
from t

这将使您不必识别重复项,然后再次弄清楚如何删除这些行。

于 2012-07-24T18:40:18.003 回答