简体   繁体   中英

How to combine these two SQL queries

I have two SQL queries with the same format, but further unrelated.

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)

and

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

Both work on their own, I would like to combine them into one where the second query adds the number of results to the main SQL result. I want to join them where caseId in the second query equals caseId of current in the main SQL. CaseId will be provided by a procedure in the final implementation.

If you mean what I think you mean:

    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

you can do join between two query based on 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM