简体   繁体   中英

This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression

I have a problem executing this query. Please see my code below.

   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

`

The code returns results for query run against a single branch. However, an error is thrown with message" Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. " When I select more than one branch. My guess is that the error occurs when the last block of select statement is run.

How can I write this query to display results for multiple branches

Please help me

By Selecting the @Branch Variable separately. Here is the Updated code sample

 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) 

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