我需要删除重复数据的帮助。
这是我的两个桌子。
1。 VL_Wohneinheiten
TABLE [dbo].[VL_Wohneinheiten](
[VLW_ID] [int] NOT NULL,
[VLW_Liegenschaft] [int] NOT NULL,
[VLW_FlatID] [nvarchar](25) NOT NULL,
[VLW_OTOID] [nvarchar](25) NOT
[ID] [int] IDENTITY(1,1) NOT NULL,
PRIMARY KEY CLUSTERED (
[VLW_ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]) ON [PRIMARY]
GO
2。 VL_Liegenschaften
TABLE [dbo].[VL_Liegenschaften](
[VLI_ID] [int] NOT NULL,
[VLI_Strasse] [nvarchar](100) NOT NULL,
[VLI_Nummer] [nvarchar](50) NOT NULL,
[VLI_PLZ] [nvarchar](10) NOT NULL,
[VLI_Ort] [nvarchar](100) NOT NULL,
[VLI_Import_flag] [tinyint] NOT NULL,
[VLI_OMDF_ID] [varchar](20) NULL, PRIMARY KEY CLUSTERED ( [VLI_ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
ON [PRIMARY]) ON [PRIMARY] GO
通过VL_Wohneinheiten.VLV_Liegenschaft = VL_Liegenschaften.VLI_ID完成这两个表之间的连接
我现在要删除所有由OTO_ID复制的数据,并且在表VL_Liegenschaften VLI_OMDF_ID中为NULL
在要查询的图像示例中,是否可以进行查询?
我已将图片设置为附加说明,但我只想更准确地说明我想做什么。
这是我尝试选择此信息的一个查询,我不确定该查询是否为真实查询,以防我该如何删除基于此显示的信息?
select t1.VLW_OTOID, t2.VLI_OMDF_ID from VL_Wohneinheiten as t1
INNER JOIN VL_Liegenschaften as t2 ON t1.VLW_Liegenschaft = t2.VLI_ID
GROUP BY t1.VLW_OTOID, t2.VLI_OMDF_ID HAVING COUNT(*) > 1
and t2.VLI_OMDF_ID IS NULL
非常感谢您的帮助。
INSERT [dbo].[VL_Wohneinheiten] ([VLW_ID], [VLW_Liegenschaft], [VLW_FlatID], [VLW_OTOID], [VLW_Koax], [VLW_Ready], [VLW_Fiber], [VLW_Status], [VLW_Status_text], [VLW_Import_flag], [VLW_Owner], [VLW_Caretaker], [VLW_Building_Type_id], [VLW_Building_Type], [ID]) VALUES (25822, 79045, N'', N'B.111.253.375.3', 0, N' ', 1, 0, N'Assigned', 1, NULL, NULL, NULL, NULL, 107184) INSERT [dbo].[VL_Wohneinheiten] ([VLW_ID], [VLW_Liegenschaft], [VLW_FlatID], [VLW_OTOID], [VLW_Koax], [VLW_Ready], [VLW_Fiber], [VLW_Status], [VLW_Status_text], [VLW_Import_flag], [VLW_Owner], [VLW_Caretaker], [VLW_Building_Type_id], [VLW_Building_Type], [ID]) VALUES (25823, 79046, N'', N'B.111.253.375.3', 0, N' ', 1, 0, N'Assigned', 1, NULL, NULL, NULL, NULL, 107185) INSERT [dbo].[VL_Wohneinheiten] ([VLW_ID], [VLW_Liegenschaft], [VLW_FlatID], [VLW_OTOID], [VLW_Koax], [VLW_Ready], [VLW_Fiber], [VLW_Status], [VLW_Status_text], [VLW_Import_flag], [VLW_Owner], [VLW_Caretaker], [VLW_Building_Type_id], [VLW_Building_Type], [ID]) VALUES (21638, 74861, N'', N'B.111.253.375.3', 0, N' ', 1, 0, N'Assigned', 1, NULL, NULL, NULL, NULL, 103000)INSERT [dbo].[VL_Wohneinheiten] ([VLW_ID], [VLW_Liegenschaft], [VLW_FlatID], [VLW_OTOID], [VLW_Koax], [VLW_Ready], [VLW_Fiber], [VLW_Status], [VLW_Status_text], [VLW_Import_flag], [VLW_Owner], [VLW_Caretaker], [VLW_Building_Type_id], [VLW_Building_Type], [ID]) VALUES (21639, 74862, N'', N'B.111.253.375.3', 0, N' ', 1, 0, N'Assigned', 1, NULL, NULL, NULL, NULL, 103001)
INSERT [dbo].[VL_Liegenschaften] ([VLI_ID], [VLI_Strasse], [VLI_Nummer], [VLI_PLZ], [VLI_Ort], [VLI_Import_flag], [VLI_OMDF_ID]) VALUES (74861, N'Zürcherstrasse', N'210', N'8500', N'Frauenfeld2', 0, NULL)
INSERT [dbo].[VL_Liegenschaften] ([VLI_ID], [VLI_Strasse], [VLI_Nummer], [VLI_PLZ], [VLI_Ort], [VLI_Import_flag], [VLI_OMDF_ID]) VALUES (74862, N'Zürcherstrasse', N'210', N'8500', N'Frauenfeld2', 0, NULL)
INSERT [dbo].[VL_Liegenschaften] ([VLI_ID], [VLI_Strasse], [VLI_Nummer], [VLI_PLZ], [VLI_Ort], [VLI_Import_flag], [VLI_OMDF_ID]) VALUES (79045, N'Zürcherstrasse', N'210A', N'8500', N'Frauenfeld2', 0, N'FRA_12')
INSERT [dbo].[VL_Liegenschaften] ([VLI_ID], [VLI_Strasse], [VLI_Nummer], [VLI_PLZ], [VLI_Ort], [VLI_Import_flag], [VLI_OMDF_ID]) VALUES (79046, N'Zürcherstrasse', N'210A', N'8500', N'Frauenfeld2', 0, N'FRA_12')
答案 0 :(得分:1)
使用row_number()
https://dbfiddle.uk/?rdbms=sqlserver_2016&fiddle=32f7806d19bbc5d77f369b1f200824e2
select * from
(select t1.VLW_OTOID, t2.VLI_OMDF_ID,row_number() over(partition by VLI_ID order by
vli_id desc) as rn from VL_Wohneinheiten as t1
INNER JOIN VL_Liegenschaften as t2 ON t1.VLW_Liegenschaft = t2.VLI_ID
and t2.VLI_OMDF_ID IS NULL) a where rn=1
要删除重复的行:
delete w from [dbo].[VL_Liegenschaften] w
inner join
(select *,row_number() over(partition by VLW_OTOID order by
VLW_Liegenschaft desc) as rn from VL_Wohneinheiten as t1
INNER JOIN VL_Liegenschaften as t2 ON t1.VLW_Liegenschaft = t2.VLI_ID
) a on a.[VLI_ID]=w.[VLI_ID] where rn<>1