我有以下SQL Server查询:
SELECT
@alloweddays as alloweddays,
@prevalloweddays as prevalloweddays,
ISNULL(COUNT(0),0) as useddays,
CASE WHEN COUNT(0) > @alloweddays THEN COUNT(0) - @alloweddays ELSE 0 END as daysinadvance,
CASE WHEN @alloweddays > COUNT(0) THEN @alloweddays - COUNT(0) ELSE 0 END as remainingdays,
temp.prevremainingdays
FROM
employee_holiday_item hi
JOIN
(SELECT
CASE WHEN @prevalloweddays > COUNT(0) THEN @prevalloweddays - COUNT(0) ELSE 0 END as prevremainingdays,
hi.employeeid
FROM
employee_holiday_item hi
WHERE
hi.employeeid = 351
AND hi.isactive = 1
AND hi.date BETWEEN @prevstartperiod AND @prevendperiod
GROUP BY
hi.employeeid) temp ON temp.employeeid = hi.employeeid
WHERE
hi.employeeid = 351
AND hi.isactive = 1
AND hi.date BETWEEN @startperiod AND @endperiod
GROUP BY
temp.prevremainingdays
如果第一个选择中没有记录,则不返回任何记录,但我希望至少获得JOIN
个选择记录。
此查询获取员工在实际年份中请求的假期。联接获取上一期间的未使用天数,因此如果员工在实际年份中没有假期请求,我希望至少看到上一期间的未使用天数。
我正在考虑进行2次完全单独的选择,然后只做UNION ALL
,但不知道它是否是最好的方法。
有任何线索吗?
答案 0 :(得分:0)
更改
...
FROM employee_holiday_item hi
JOIN(
SELECT
CASE WHEN @prevalloweddays ...
到
...
FROM employee_holiday_item hi
RIGHT OUTER JOIN(
SELECT
CASE WHEN @prevalloweddays ...
这就是说"从JOIN
的右侧拿走所有内容,无论如何都返回...如果JOIN
左侧返回结果,则返回它们"