当我运行以下查询时:
SELECT
CASE
WHEN AT_EMPSCHEDULE.START1 <> '01/01/1900'
AND AT_EMPSCHEDULE.START2 <> '01/01/1900'
AND AT_EMPSCHEDULE.END2 <> '01/01/1900'
AND AT_EMPSCHEDULE.LEAVECODE =''
AND NOT EXISTS(SELECT PDATE
FROM HR_PUBHOLIDAY
WHERE PDATE=AT_EMPSCHEDULE.TRANDATE)
AND DATEPART(WEEKDAY,AT_EMPSCHEDULE.TRANDATE) <> 1
THEN
CAST((SELECT HIS_GENSALARYD.BASESALARY/HIS_GENSALARYD.WORKDAY
FROM HIS_GENSALARYD
WHERE (AT_EMPSCHEDULE.TRANDATE BETWEEN HIS_GENSALARYD.PAYFROM
AND HIS_GENSALARYD.PAYTO
AND HIS_GENSALARYD.EMPCODE = AT_EMPSCHEDULE.EMPCODE)) AS DECIMAL(10, 2))
ELSE 0
END AS DAILYSALARY
FROM
HIS_GENSALARY
INNER JOIN
AT_EMPSCHEDULE ON HIS_GENSALARY.EMPCODE = AT_EMPSCHEDULE.EMPCODE
AND MONTH(TRANDATE) = HIS_GENSALARY.INMONTH
AND YEAR(TRANDATE) = HIS_GENSALARY.INYEAR
WHERE
HIS_GENSALARY.EMPCODE = HIS_GENSALARY.EMPCODE
我收到消息:
子查询返回的值超过1。当子查询遵循=,!=,&lt;,&lt; =,&gt;,&gt; =或子查询用作表达式时,不允许这样做。
答案 0 :(得分:0)
问题在于:
(SELECT HIS_GENSALARYD.BASESALARY/HIS_GENSALARYD.WORKDAY
FROM HIS_GENSALARYD
WHERE AT_EMPSCHEDULE.TRANDATE BETWEEN HIS_GENSALARYD.PAYFROM
AND HIS_GENSALARYD.PAYTO
AND HIS_GENSALARYD.EMPCODE = AT_EMPSCHEDULE.EMPCODE)
您已用作表达式。根据错误消息,它必须永远生成一个结果,您已经使用过它;即它必须永远不会返回多行,但至少有一个调用返回了多行。
要强制它只返回一行,请对其应用聚合函数;一个明显的选择是max()
:
(SELECT MAX(HIS_GENSALARYD.BASESALARY/HIS_GENSALARYD.WORKDAY)
FROM HIS_GENSALARYD
WHERE AT_EMPSCHEDULE.TRANDATE BETWEEN HIS_GENSALARYD.PAYFROM
AND HIS_GENSALARYD.PAYTO
AND HIS_GENSALARYD.EMPCODE = AT_EMPSCHEDULE.EMPCODE)