我正在尝试执行子选择查询,但它给了我一个错误,我不确定我是否可以执行这种逻辑。
SELECT (SELECT SERV_ACCT, ACCOUNT, SERV_ADDRESS
FROM tblAPSData
JOIN tblMEP_Meters
ON tblMEP_Meters.ServiceAccount = tblAPSData.SERV_ACCT
WHERE SERV_Acct = ServiceAccount
AND INV_DATE > DATEADD(month, -6, getdate())
GROUP BY SERV_ACCT, ACCOUNT, SERV_ADDRESS
HAVING count(SERV_ACCT) = 1) AS AccountMeter
FROM tblMEP_Customers
这是错误消息:Msg 116, Level 16, State 1, Line 48 当子查询没有引入 EXISTS 时,只能在选择列表中指定一个表达式。
我也有这段代码编译没有任何问题,我使用相同的逻辑,但不知道为什么它不起作用。此代码在我运行时有效:
SELECT SiteName = tblMEP_Sites.Name, BillingMonth = DateTimeStamp,
ElectricConsumption = (SELECT SUM(Consumption)
FROM tblMEP_Meters (nolock)
JOIN tblMEP_MonthlyData (nolock)
ON MeterID = tblMEP_Meters.ID
WHERE SiteID = tblMEP_Sites.ID
AND Type = 1
AND BillingMonth = DATEADD(month, -6, getdate()))
FROM tblMEP_Customers