从SQL Server视图中删除重复记录
如何从视图中删除重复记录?我需要保持他们在物理表,但在看法,我不想重复从SQL Server视图中删除重复记录
这里是我使用的查询:
CREATE VIEW myview AS
SELECT DISTINCT *
FROM [roug].[dbo].[Table_1]
ORDER BY id
为表:
id| name age ----------
c1 ann 12
u2 joe 15
c1 ann 12
c1 ann 12
u5 dev 13
u3 Jim 16
u3 Jim 16
回答:
您可以使用DISTINCT或ROW_NUMBER()筛选
create view myview as WITH CTE
AS
(
SELECT
RN = ROW_NUMBER() OVER(PARTITION BY [Id],[Name],[Age] ORDER BY ID),
*
FROM [roug].[dbo].[Table_1]
)
SELECT
[Id],[Name],[Age]
FROM CTE
WHERE RN = 1
回答:
如果你想删除的数据,那么你应该b e在源表中执行它而不是视图。标准的去重方法是通过cte。尝试
; WITH cte
AS (SELECT id
, name
, age
, ROW_NUMBER() OVER (PARTITION BY id, name, age ORDER BY id) RN
FROM Table_1
)
DELETE FROM cte
WHERE RN > 1
取决于您是要删除实际数据,还是只是不显示它在视图中。
以上是 从SQL Server视图中删除重复记录 的全部内容, 来源链接: utcz.com/qa/262913.html