我有一个这样的存储过程:
ALTER PROCEDURE [dbo].[Driverperformance]
@Ecode NVARCHAR(50),
@startdate DATETIME,
@enddate DATETIME
AS
BEGIN
DECLARE @date1 DATETIME = CONVERT(DATETIME, @startdate + ' 00:01:00.000', 120);
DECLARE @date2 DATETIME = CONVERT(DATETIME, @enddate + ' 23:23:59.000', 120);
SELECT e.Ecode
, CAST(q.dtime AS DATE) AS Date
, e.Ename
, COUNT(q.Ecode) CntEcode
, (
SELECT COUNT(*)
FROM Transaction_tbl
WHERE dtime >= '' + @date1 + ''
AND dtime <= '' + @date2 + ''
AND DelEcode = @Ecode
GROUP BY CAST(dtime AS DATE)
)
FROM EmployeeMaster_tbl e
JOIN Transaction_tbl q ON e.Ecode = q.Ecode
WHERE q.Ecode = @Ecode
AND dtime >= '' + @date1 + ''
AND dtime <= '' + @date2 + ''
GROUP BY
e.Ecode
, e.Ename
, CAST(q.dtime AS DATE)
ORDER BY CAST(q.dtime AS DATE)
END
执行此查询时出现如下错误:
子查询返回的值超过1。这是不允许的 子查询跟随=,!=,&lt;,&lt; =,&gt;,&gt; =或当子查询用作 表达。
答案 0 :(得分:0)
在您的查询中,您有:
(
SELECT COUNT(*)
FROM Transaction_tbl
WHERE dtime >= '' + @date1 + ''
AND dtime <= '' + @date2 + ''
AND DelEcode = @Ecode
GROUP BY CAST(dtime AS DATE)
)
作为专栏。以上因为GROUP BY将为您的每一行返回多行。这在SQL中是不可行的。