簡體   English   中英

如何在SQL中聯接多個select語句並在不同的列中顯示

[英]How to join multiple select statements in SQL and display in different colums

我編寫了一個查詢,該查詢將兩個表連接起來,顯示總數no. of appointments 使用union關鍵字no. of appointmentsleads的查詢,我希望此查詢的結果顯示在兩個不同的列中,我對此有一個很難理解的描述

SELECT COUNT(FilteredAppointment.createdbyname) AS Appointment
FROM  FilteredBusinessUnit 
INNER JOIN FilteredSystemUser 
    ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid 
INNER JOIN FilteredAppointment 
    ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
UNION
SELECT COUNT(FilteredLead.fullname) AS Lead
FROM  FilteredBusinessUnit 
INNER JOIN FilteredSystemUser 
    ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid 
INNER JOIN FilteredLead 
    ON FilteredSystemUser.systemuserid = FilteredLead.createdby
WHERE (FilteredBusinessUnit.name IN (@Branch))

我想要的結果是:

CRITERIA | Appointment | Leads
Total    | 200         | 123

聯盟不是正確的方法。 Union合並或合並為具有相同數量的列和數據類型的結果集。 我會做這樣的事情。

SELECT 
    (   SELECT COUNT(FilteredAppointment.createdbyname)
        FROM  FilteredBusinessUnit INNER JOIN
              FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
              FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
        WHERE (FilteredBusinessUnit.name IN (@Branch))
    ) AS Appointment ,
    (   SELECT COUNT(FilteredLead.fullname)
        FROM  FilteredBusinessUnit INNER JOIN
              FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
              FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
        WHERE (FilteredBusinessUnit.name IN (@Branch))
    ) AS Lead

為了回應您的評論,以便能夠將數字與其他分支進行比較,我會嘗試類似的方法。

SELECT  FilteredBusinessUnit.name , 
        SUM(CASE WHEN FilteredAppointment.createdbyname IS NOT NULL THEN 1 ELSE 0 END) AS Appointments,
        SUM(CASE WHEN FilteredAppointment.fullname IS NOT NULL THEN 1 ELSE 0 END) AS Leads
FROM    FilteredBusinessUnit 
        INNER JOIN FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid 
        LEFT OUTER JOIN FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
        LEFT OUTER JOIN FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
WHERE   (FilteredBusinessUnit.name IN (@Branch1, @Branch2)) 
GROUP BY FilteredBusinessUnit.name

如果要查看所有分支,可以忽略where子句。

通過cross join而不是union執行此操作:

select 'Total', Appointment, Leads
from (SELECT COUNT(FilteredAppointment.createdbyname) AS Appointment
      FROM  FilteredBusinessUnit INNER JOIN
            FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
            FilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
     ) c1 cross join
     (SELECT COUNT(FilteredLead.fullname) AS Leads
      FROM  FilteredBusinessUnit INNER JOIN
            FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid INNER JOIN
            FilteredLead ON FilteredSystemUser.systemuserid = FilteredLead.createdby
      WHERE (FilteredBusinessUnit.name IN (@Branch))
     ) t

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM