我在执行此查询时遇到问题。请参阅下面的代码。
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,
(选择名称 来自FilteredBusinessUnit WHERE(FilteredBusinessUnit.name IN(@Branch)))AS BRANCH
`
代码返回针对单个分支运行查询的结果。但是,抛出错误消息“子查询返回的值超过1。当子查询跟随=,!=,<,< =,>,> =或子查询用作表达。“当我选择多个分支时。我的猜测是,当运行select语句的最后一个块时会发生错误。
如何编写此查询以显示多个分支的结果
请帮帮我
答案 0 :(得分: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)