有没有办法在嵌套的 WITH 语句中包含 DELETE FROM 语句?
Is there a way to include a DELETE FROM statement in a nested WITH statement?
我试图通过创建另一个 table 来删除重复项,但我无法包含 delete from 语句。我在 SQL 服务器中有代码,但我正在尝试将其转换为 Oracle。我尝试用 delete from 替换 select 但出现错误:缺少 SELECT 关键字。
代码如下:
WITH RowNumCTE AS(
SELECT t.* ,
ROW_NUMBER() OVER (
PARTITION BY ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
ORDER BY
UniqueID
) t
FROM nashvillehousing t
--Order by parcelid;
)
SELECT *
FROM RowNumCTE
WHERE t > 1;
示例数据如下:
预期结果应该是删除了 104 个重复条目。
可以关联ROWID伪列:
DELETE FROM nashvillehousing
WHERE ROWID IN (
SELECT ROWID
FROM (
SELECT ROW_NUMBER() OVER (
PARTITION BY ParcelID, PropertyAddress, SalePrice, SaleDate, LegalReference
ORDER BY UniqueID
) AS rn
FROM nashvillehousing
)
WHERE rn > 1
)
如果你真的想使用子查询分解子句(WITH
)那么:
DELETE FROM nashvillehousing
WHERE ROWID IN (
WITH sqfc (rn) AS (
SELECT ROW_NUMBER() OVER (
PARTITION BY ParcelID, PropertyAddress, SalePrice, SaleDate, LegalReference
ORDER BY UniqueID
)
FROM nashvillehousing
)
SELECT ROWID
FROM sqfc
WHERE rn > 1
)
我试图通过创建另一个 table 来删除重复项,但我无法包含 delete from 语句。我在 SQL 服务器中有代码,但我正在尝试将其转换为 Oracle。我尝试用 delete from 替换 select 但出现错误:缺少 SELECT 关键字。
代码如下:
WITH RowNumCTE AS(
SELECT t.* ,
ROW_NUMBER() OVER (
PARTITION BY ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
ORDER BY
UniqueID
) t
FROM nashvillehousing t
--Order by parcelid;
)
SELECT *
FROM RowNumCTE
WHERE t > 1;
示例数据如下:
预期结果应该是删除了 104 个重复条目。
可以关联ROWID伪列:
DELETE FROM nashvillehousing
WHERE ROWID IN (
SELECT ROWID
FROM (
SELECT ROW_NUMBER() OVER (
PARTITION BY ParcelID, PropertyAddress, SalePrice, SaleDate, LegalReference
ORDER BY UniqueID
) AS rn
FROM nashvillehousing
)
WHERE rn > 1
)
如果你真的想使用子查询分解子句(WITH
)那么:
DELETE FROM nashvillehousing
WHERE ROWID IN (
WITH sqfc (rn) AS (
SELECT ROW_NUMBER() OVER (
PARTITION BY ParcelID, PropertyAddress, SalePrice, SaleDate, LegalReference
ORDER BY UniqueID
)
FROM nashvillehousing
)
SELECT ROWID
FROM sqfc
WHERE rn > 1
)