2

我写了一个查询,它连接了两个显示总数no. of appointmentsleads使用union关键字的表,我希望这个查询的结果显示在两个不同的列中,我有一个艰难的描述

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
4

2 回答 2

0

联合不是正确的方法。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 子句。

于 2013-09-11T15:21:33.550 回答
0

使用 across 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
于 2013-09-11T15:21:49.760 回答