根据 min (row_id) 删除并 select
Delete and select based on min (row_id)
我正在尝试通过从 table 中选择 MIN(row_id)
来删除记录,但查询不起作用。
它抛出这个错误:
The ORDER BY
clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
这是我的查询:
DELETE FROM c_dim
WHERE MIN(c_key) in (
SELECT c_key, c_code
FROM c_dim
WHERE flag = 'X' and c_code IN (
SELECT c_code
FROM c_dim
WHERE flag = 'X'
GROUP BY c_code HAVING COUNT(c_code)>1
)
ORDER BY c_code
)
我想你所有的子查询都可以用这个简单的替换
;WITH CTE AS
(
SELECT * , ROW_NUMBER() OVER (PARTITION BY c_code ORDER BY c_key) rn
FROM c_dim
WHERE flag = 'X'
)
DELETE FROM CTE WHERE rn > 1
几乎可以肯定还有其他方法可以在没有所有子查询的情况下对其进行优化,但错误消息非常清楚。用几个例子来说明为什么你会收到错误:
这应该可行,因为您不会在子查询中使用 ORDER BY。
select c_key, c_code FROM c_dim where flag = 'X'
and c_code IN
(SELECT c_code
FROM c_dim
WHERE flag = 'X'
GROUP BY c_code HAVING COUNT(c_code) > 1 )
ORDER BY c_code
这应该不起作用,因为您在子查询中有 ORDER BY:
Select * FROM c_dim WHERE min(c_key)
in(select c_key, c_code FROM c_dim where flag = 'X'
and c_code IN
(SELECT c_code
FROM c_dim
WHERE flag = 'X'
GROUP BY c_code HAVING COUNT(c_code) > 1 )
ORDER BY c_code)
因此,要修复所写的 DELETE,只需将 ORDER BY 从子查询中取出,因为删除命令完全不需要它:
DELETE FROM c_dim WHERE min(c_key)
in(select c_key, c_code FROM c_dim where flag = 'X'
and c_code IN
(SELECT c_code
FROM c_dim
WHERE flag = 'X'
GROUP BY c_code HAVING COUNT(c_code) > 1 )
)
我想 c_key 是独一无二的?
尝试这样的事情:
delete from c_dim where c_key = min(c_key)
and rownum = 1;/* if you want the only first */
现在,我告诉你这不是一个好的删除请求,但她可以解决你的问题。
我正在尝试通过从 table 中选择 MIN(row_id)
来删除记录,但查询不起作用。
它抛出这个错误:
The
ORDER BY
clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.
这是我的查询:
DELETE FROM c_dim
WHERE MIN(c_key) in (
SELECT c_key, c_code
FROM c_dim
WHERE flag = 'X' and c_code IN (
SELECT c_code
FROM c_dim
WHERE flag = 'X'
GROUP BY c_code HAVING COUNT(c_code)>1
)
ORDER BY c_code
)
我想你所有的子查询都可以用这个简单的替换
;WITH CTE AS
(
SELECT * , ROW_NUMBER() OVER (PARTITION BY c_code ORDER BY c_key) rn
FROM c_dim
WHERE flag = 'X'
)
DELETE FROM CTE WHERE rn > 1
几乎可以肯定还有其他方法可以在没有所有子查询的情况下对其进行优化,但错误消息非常清楚。用几个例子来说明为什么你会收到错误:
这应该可行,因为您不会在子查询中使用 ORDER BY。
select c_key, c_code FROM c_dim where flag = 'X'
and c_code IN
(SELECT c_code
FROM c_dim
WHERE flag = 'X'
GROUP BY c_code HAVING COUNT(c_code) > 1 )
ORDER BY c_code
这应该不起作用,因为您在子查询中有 ORDER BY:
Select * FROM c_dim WHERE min(c_key)
in(select c_key, c_code FROM c_dim where flag = 'X'
and c_code IN
(SELECT c_code
FROM c_dim
WHERE flag = 'X'
GROUP BY c_code HAVING COUNT(c_code) > 1 )
ORDER BY c_code)
因此,要修复所写的 DELETE,只需将 ORDER BY 从子查询中取出,因为删除命令完全不需要它:
DELETE FROM c_dim WHERE min(c_key)
in(select c_key, c_code FROM c_dim where flag = 'X'
and c_code IN
(SELECT c_code
FROM c_dim
WHERE flag = 'X'
GROUP BY c_code HAVING COUNT(c_code) > 1 )
)
我想 c_key 是独一无二的?
尝试这样的事情:
delete from c_dim where c_key = min(c_key)
and rownum = 1;/* if you want the only first */
现在,我告诉你这不是一个好的删除请求,但她可以解决你的问题。