0

我在执行此查询时遇到问题。请在下面查看我的代码。

   SELECT        (SELECT        COUNT(FilteredAppointment.activitytypecodename) AS Expr1
                      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.leadid) AS Expr1
                           FROM            FilteredBusinessUnit AS FilteredBusinessUnit_7 INNER JOIN
                                                     FilteredSystemUser AS FilteredSystemUser_7 ON FilteredBusinessUnit_7.businessunitid = FilteredSystemUser_7.businessunitid INNER JOIN
                                                     FilteredLead ON FilteredSystemUser_7.systemuserid = FilteredLead.createdby
                           WHERE        (FilteredBusinessUnit_7.name IN (@Branch)) AND (FilteredLead.new_referraltypename = 'Bank Staff')) AS Bank_Staff_Referral,
                         (SELECT        COUNT(FilteredLead_3.leadid) AS Expr1
                           FROM            FilteredBusinessUnit AS FilteredBusinessUnit_6 INNER JOIN
                                                     FilteredSystemUser AS FilteredSystemUser_6 ON FilteredBusinessUnit_6.businessunitid = FilteredSystemUser_6.businessunitid INNER JOIN
                                                     FilteredLead AS FilteredLead_3 ON FilteredSystemUser_6.systemuserid = FilteredLead_3.createdby
                           WHERE        (FilteredBusinessUnit_6.name IN (@Branch)) AND (FilteredLead_3.new_referraltypename = 'Existing Customer')) AS Customer_Referral,
                         (SELECT        COUNT(Filterednew_discoveryinterview.activityid) AS Expr1
                           FROM            FilteredBusinessUnit AS FilteredBusinessUnit_5 INNER JOIN
                                                     FilteredSystemUser AS FilteredSystemUser_5 ON FilteredBusinessUnit_5.businessunitid = FilteredSystemUser_5.businessunitid INNER JOIN
                                                     Filterednew_discoveryinterview ON FilteredSystemUser_5.systemuserid = Filterednew_discoveryinterview.createdby
                           WHERE        (FilteredBusinessUnit_5.name IN (@Branch))) AS Discovery_Interview,
                         (SELECT        COUNT(FilteredLead_2.leadid) AS Expr1
                           FROM            FilteredBusinessUnit AS FilteredBusinessUnit_4 INNER JOIN
                                                     FilteredSystemUser AS FilteredSystemUser_4 ON FilteredBusinessUnit_4.businessunitid = FilteredSystemUser_4.businessunitid INNER JOIN
                                                     FilteredLead AS FilteredLead_2 ON FilteredSystemUser_4.systemuserid = FilteredLead_2.createdby
                           WHERE        (FilteredBusinessUnit_4.name IN (@Branch))) AS Generated_Leads,

(SELECT name FROM FilteredBusinessUnit WHERE (FilteredBusinessUnit.name IN (@Branch))) AS BRANCH

`

代码返回针对单个分支运行的查询结果。但是,会抛出错误消息“子查询返回超过 1 个值。当子查询跟在 =、!=、<、<=、>、>= 之后或将子查询用作表达式时,这是不允许的。” 当我选择了多个分支。我的猜测是,当 select 语句的最后一个块运行时会发生错误。

如何编写此查询以显示多个分支的结果

请帮我

4

1 回答 1

1

通过单独选择@Branch 变量。这是更新的代码示例

 SELECT   MQ.Name AS BranchName,
      (SELECT COUNT(FilteredAppointment.activitytypecodename) AS Expr1
                  FROM  FilteredBusinessUnit 
                  INNER JOIN FilteredSystemUser ON FilteredBusinessUnit.businessunitid = FilteredSystemUser.businessunitid 
                  INNER JOINFilteredAppointment ON FilteredSystemUser.systemuserid = FilteredAppointment.createdby
                  WHERE (FilteredBusinessUnit.name = MQ.Name)) AS Appointment,
                  (SELECT COUNT(FilteredLead.leadid) AS Expr1
                       FROM  FilteredBusinessUnit AS FilteredBusinessUnit_7 
                       INNER JOIN FilteredSystemUser AS FilteredSystemUser_7 ON FilteredBusinessUnit_7.businessunitid = FilteredSystemUser_7.businessunitid 
                       INNER JOIN FilteredLead ON FilteredSystemUser_7.systemuserid = FilteredLead.createdby
                       WHERE  (FilteredBusinessUnit_7.name = MQ.Name) AND (FilteredLead.new_referraltypename = 'Bank Staff')) AS Bank_Staff_Referral,
                     (SELECT COUNT(FilteredLead_3.leadid) AS Expr1
                       FROM            FilteredBusinessUnit AS FilteredBusinessUnit_6 INNER JOIN
                                                 FilteredSystemUser AS FilteredSystemUser_6 ON FilteredBusinessUnit_6.businessunitid = FilteredSystemUser_6.businessunitid INNER JOIN
                                                 FilteredLead AS FilteredLead_3 ON FilteredSystemUser_6.systemuserid = FilteredLead_3.createdby
                       WHERE        (FilteredBusinessUnit_6.name = MQ.Name) AND (FilteredLead_3.new_referraltypename = 'Existing Customer')) AS Customer_Referral,
                     (SELECT        COUNT(Filterednew_discoveryinterview.activityid) AS Expr1
                       FROM            FilteredBusinessUnit AS FilteredBusinessUnit_5 INNER JOIN
                                                 FilteredSystemUser AS FilteredSystemUser_5 ON FilteredBusinessUnit_5.businessunitid = FilteredSystemUser_5.businessunitid INNER JOIN
                                                 Filterednew_discoveryinterview ON FilteredSystemUser_5.systemuserid = Filterednew_discoveryinterview.createdby
                       WHERE        (FilteredBusinessUnit_5.name = MQ.Name)) AS Discovery_Interview,
                     (SELECT        COUNT(FilteredLead_2.leadid) AS Expr1
                       FROM            FilteredBusinessUnit AS FilteredBusinessUnit_4 INNER JOIN
                                                 FilteredSystemUser AS FilteredSystemUser_4 ON FilteredBusinessUnit_4.businessunitid = FilteredSystemUser_4.businessunitid INNER JOIN
                                                 FilteredLead AS FilteredLead_2 ON FilteredSystemUser_4.systemuserid = FilteredLead_2.createdby
                       WHERE        (FilteredBusinessUnit_4.name = MQ.Name)) AS Generated_Leads
FROM FilteredBusinessUnit MQ--I Assume this is where the Branch Name  Is kept
WHERE MQ.Name In (@Branch) 
于 2013-09-14T19:24:29.390 回答