gpt4 book ai didi

sql-server - T-SQL : Can I query one view for values and then query a second view only for values that don't exist in the first?

转载 作者:行者123 更新时间:2023-12-04 07:34:37 25 4
gpt4 key购买 nike

DROP TABLE IF EXISTS ..Players
DROP TABLE IF EXISTS ..FreeAgents

CREATE TABLE [dbo].[FreeAgents](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[Position] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_FreeAgents] PRIMARY KEY CLUSTERED
(
[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

CREATE TABLE [dbo].[Players](
[Id] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[Position] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Players] PRIMARY KEY CLUSTERED
(
[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
SET IDENTITY_INSERT [dbo].[FreeAgents] ON
GO
INSERT [dbo].[FreeAgents] ([Id], [FirstName], [LastName], [Position]) VALUES (2, N'Julian', N'Edelman', N'WideReceiver')
GO
INSERT [dbo].[FreeAgents] ([Id], [FirstName], [LastName], [Position]) VALUES (3, N'Dez', N'Bryant', N'WideReceiver')
GO
INSERT [dbo].[FreeAgents] ([Id], [FirstName], [LastName], [Position]) VALUES (4, N'Brandon', N'Jacobs', N'DefensiveEnd')
GO
SET IDENTITY_INSERT [dbo].[FreeAgents] OFF
GO
SET IDENTITY_INSERT [dbo].[Players] ON
GO
INSERT [dbo].[Players] ([Id], [FirstName], [LastName], [Position]) VALUES (1, N'Tom', N'Brady', N'Quarterback')
GO
INSERT [dbo].[Players] ([Id], [FirstName], [LastName], [Position]) VALUES (2, N'Cam', N'Newton', N'Quarterback')
GO
INSERT [dbo].[Players] ([Id], [FirstName], [LastName], [Position]) VALUES (3, N'Julian', N'Edelman', N'WideReceiver')
GO
SET IDENTITY_INSERT [dbo].[Players] OFF
GO

SELECT FirstName, LastName, Position FROM ..Players
UNION
SELECT FirstName, LastName, Position FROM ..FreeAgents
上面的代码返回五行。这是对的。




位置


布兰登
雅各布斯
防守端

凸轮
牛顿
四分卫

德兹
布莱恩特
宽接收器

朱利安
爱德曼
宽接收器

汤姆
布雷迪
四分卫


我的问题:
  • 上面的代码片段查询并联合了两个物理表。
  • 我的实际情况涉及两种计算密集型 View 。

  • 从概念上讲,我想返回与 UNION 相同的结果集。返回。
  • 除了我不想查询第二个表中已经存在于第一个表中的行。

  • 重点:对于小表,这没有明显的区别。 SQL Server 引擎完全能够使用 UNION 过滤掉重复项。条款。
  • 但在我的实际情况中,我不会查询两个小的物理表。我正在查询两个计算密集型 View 。

  • 最佳答案

    如果您有一组不是计算密集型的键(例如您的示例中的 id col),我认为您最好使用临时表来存储来自第一个 View 的结果,然后将所有它与第二个 View 联合起来,检查它没有存在于临时表中。

    SELECT * FROM view1
    INTO #v1;

    SELECT * FROM #v1
    UNITON ALL
    SELECT * FROM view2
    WHERE NOT EXISTS(SELECT 1 FROM #v1 WHERE #v1.id = view2.id)
    第二个 View 应该只为丢失的记录执行计算。
    使用 WITH声明可以更好,但我不确定 - 这需要在真实数据上进行测试。
    WITH v1 AS (SELECT * FROM view1)
    SELECT * FROM v1
    UNION ALL
    SELECT * FROM view2
    WHERE NOT EXISTS (
    SELECT 1 FROM v1
    WHERE v1.id = view2.id -- Set duplicate condition here
    )

    关于sql-server - T-SQL : Can I query one view for values and then query a second view only for values that don't exist in the first?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67795881/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com