如何从[QUERY]中删除DELETE?

时间:2013-12-13 14:43:58

标签: sql sql-server sql-server-2008

嗨,我对mssql不熟悉,我已经习惯了Oracle。我正在尝试从子查询中删除一个特定的行,但是mssql并不真的喜欢子查询。

以下是查询:

DELETE FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY column1 ORDER BY column1) row FROM randomtable) a
WHERE a.row = 1

有没有办法让它发挥作用?

在Oracle中,我可以在查询中获取所有内容,因为我可以使用rownum = 1。

3 个答案:

答案 0 :(得分:3)

你快到了

DELETE a
FROM   (SELECT *,
               ROW_NUMBER() OVER (PARTITION BY column1 ORDER BY column1) row
        FROM   randomtable) a
WHERE  a.row = 1 

虽然我更喜欢CTE语法

WITH a
     AS (SELECT *,
                ROW_NUMBER() OVER (PARTITION BY column1 ORDER BY column1) row
         FROM   randomtable)
DELETE FROM a
WHERE  a.row = 1 

答案 1 :(得分:3)

您没有在要删除的内容中指定表别名。

DELETE a FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY column1 ORDER BY column1) row FROM randomtable) a
WHERE a.row = 1

答案 2 :(得分:1)

写别名DELETE a FROM ...