如何结合这两个SQL查询

时间:2018-09-11 15:14:16

标签: sql sql-server

我有两个具有相同格式的SQL查询,但又彼此无关。

SELECT TOP (100) PERCENT 
    dbo.[Case].Id AS CaseId,
    dbo.[Case].State, dbo.[Case].StartDate, dbo.[Case].Description,
    dbo.[Case].OrderNumber, dbo.[Case].LastUpdatedDate,
    dbo.[User].Name AS CompanyUserName, 
    User_1.Name AS ResponsibleCompanyUserName,
    User_2.Name AS CustomerName, dbo.Property.Address,
    dbo.[Case].CompanyId, dbo.[Case].PartnerSettingId
FROM
    dbo.[User] AS User_2
INNER JOIN 
    dbo.Property ON User_2.Id = dbo.Property.UserId
RIGHT OUTER JOIN 
    dbo.[Case] ON dbo.Property.Id = dbo.[Case].PropertyId
LEFT OUTER JOIN 
    dbo.[User] AS User_1 ON dbo.[Case].ResponsibleCompanyUserId = User_1.Id
LEFT OUTER JOIN 
    dbo.[User] ON dbo.[Case].CompanyUserId = dbo.[User].Id
WHERE
    (dbo.[Case].DisableNotifications = 0)
    AND (dbo.[Case].IsDeleted IS NULL)
    AND (dbo.[Case].OrderNumber IS NOT NULL)

SELECT TOP (100) PERCENT 
    dbo.[Case].Id AS CaseId,
    dbo.[Case].State, dbo.[Case].StartDate, dbo.[Case].Description,
    dbo.[Case].OrderNumber, dbo.[Case].LastUpdatedDate,
    dbo.[User].Name AS CompanyUserName, 
    User_1.Name AS ResponsibleCompanyUserName,
    User_2.Name AS CustomerName, dbo.Property.Address,
    dbo.[Case].CompanyId, dbo.[Case].PartnerSettingId
FROM
    [dbo].[CaseMessage] 
WHERE
    UserId != 55 
    AND [CaseId] = 245 
    AND [ReadTimestamp] IS NULL

两者都是独立工作的,我想将它们组合成一个,其中第二个查询将结果数添加到主SQL结果中。我想加入它们,其中第二个查询中的caseId等于主SQL中当前的caseId。 CaseId将由最终实现中的过程提供。

2 个答案:

答案 0 :(得分:1)

如果你的意思是我想的意思:

    SELECT        TOP (100) PERCENT dbo.[Case].Id AS CaseId, dbo.[Case].State, dbo.[Case].StartDate, dbo.[Case].Description, dbo.[Case].OrderNumber, dbo.[Case].LastUpdatedDate, dbo.[User].Name AS CompanyUserName, 
                         User_1.Name AS ResponsibleCompanyUserName, User_2.Name AS CustomerName, dbo.Property.Address, dbo.[Case].CompanyId, dbo.[Case].PartnerSettingId
FROM            dbo.[User] AS User_2 INNER JOIN
                         dbo.Property ON User_2.Id = dbo.Property.UserId RIGHT OUTER JOIN
                         dbo.[Case] ON dbo.Property.Id = dbo.[Case].PropertyId LEFT OUTER JOIN
                         dbo.[User] AS User_1 ON dbo.[Case].ResponsibleCompanyUserId = User_1.Id LEFT OUTER JOIN
                         dbo.[User] ON dbo.[Case].CompanyUserId = dbo.[User].Id
WHERE        (dbo.[Case].DisableNotifications = 0) AND (dbo.[Case].IsDeleted IS NULL) AND (dbo.[Case].OrderNumber IS NOT NULL)


UNION ALL

    SELECT        TOP (100) PERCENT dbo.[Case].Id AS CaseId, dbo.[Case].State, dbo.[Case].StartDate, dbo.[Case].Description, dbo.[Case].OrderNumber, dbo.[Case].LastUpdatedDate, dbo.[User].Name AS CompanyUserName, 
                         User_1.Name AS ResponsibleCompanyUserName, User_2.Name AS CustomerName, dbo.Property.Address, dbo.[Case].CompanyId, dbo.[Case].PartnerSettingId
FROM [dbo].[CaseMessage] where UserId != 55 and [CaseId] = 245 and [ReadTimestamp] IS NULL

答案 1 :(得分:0)

您可以基于CaseId

在两个查询之间进行联接
with cte1 as
    (
    SELECT        TOP (100) PERCENT dbo.[Case].Id AS CaseId, dbo.[Case].State, dbo.[Case].StartDate, dbo.[Case].Description, dbo.[Case].OrderNumber, dbo.[Case].LastUpdatedDate, dbo.[User].Name AS CompanyUserName, 
                             User_1.Name AS ResponsibleCompanyUserName, User_2.Name AS CustomerName, dbo.Property.Address, dbo.[Case].CompanyId, dbo.[Case].PartnerSettingId
    FROM            dbo.[User] AS User_2 INNER JOIN
                             dbo.Property ON User_2.Id = dbo.Property.UserId RIGHT OUTER JOIN
                             dbo.[Case] ON dbo.Property.Id = dbo.[Case].PropertyId LEFT OUTER JOIN
                             dbo.[User] AS User_1 ON dbo.[Case].ResponsibleCompanyUserId = User_1.Id LEFT OUTER JOIN
                             dbo.[User] ON dbo.[Case].CompanyUserId = dbo.[User].Id
    WHERE        (dbo.[Case].DisableNotifications = 0) AND (dbo.[Case].IsDeleted IS NULL) AND (dbo.[Case].OrderNumber IS NOT NULL)
    ),
    cte2 as
    (select TOP (100) PERCENT dbo.[Case].Id AS CaseId, dbo.[Case].State, dbo.[Case].StartDate, dbo.[Case].Description, dbo.[Case].OrderNumber, dbo.[Case].LastUpdatedDate, dbo.[User].Name AS CompanyUserName, 
                     User_1.Name AS ResponsibleCompanyUserName, User_2.Name AS CustomerName, dbo.Property.Address, dbo.[Case].CompanyId, dbo.[Case].PartnerSettingId FROM [dbo].[CaseMessage] where UserId != 55 and [CaseId] = 245 and [ReadTimestamp] IS NULL
    ) select cte1.*,cte2.* from cte1 join cte2 on cte1.CaseId=cte2.CaseId