SELECT
SUBSTR(x.leave_from_date,0,4) leaveFromDate,
(SELECT b.description
FROM T4DEPT b
WHERE b.dept_no = x.dept_no) deptDesc,
x.emp_no empNo,
(SELECT a.user_name
FROM T4USERS a
WHERE a.user_id = x.emp_no) empName,
(SELECT NVL(basic_leave,0) FROM T4USERS WHERE x.emp_no=user_id) leave_total,
SUM(x.leave_days) leave_real,
(SELECT NVL(basic_leave,0) FROM T4USERS WHERE x.emp_no=user_id) -
(SELECT NVL (SUM (c.leave_days), 0)
FROM ET_LEAVE c
WHERE x.emp_no = c.emp_no
AND TO_CHAR (TO_DATE (c.leave_from_date,'yyyymmdd'),'yyyy') = TO_CHAR (SYSDATE,'yyyy')
AND c.leave_status = 'C'
AND c.leave_type NOT IN ('05','06')) leftDays
FROM ET_LEAVE x
WHERE 1=1
GROUP BY SUBSTR(x.leave_from_date,0,4),x.emp_no, x.dept_no
order by leaveFromDate, empNo
위와 같은 식으로 휴가통계를 내는 쿼리를 짜는데.
중간에 AND TO_CHAR (TO_DATE (c.leave_from_date,'yyyymmdd'),'yyyy') = TO_CHAR (SYSDATE,'yyyy')
이부분에서 SYSDATE가 아니라 x의 년도를 넣어야 원하는 값들이 나오는데
그룹절을 쓰다보니다 저 안에 SUBSTR(x.leave_from_date,0,4) 를 넣으면 에러가 나네요..
혹시 왜 그런지 알 수 있을까요??
-- 조인과 그룹바이에 대한 이해 부족으로 인해 간단한걸 어렵게 하셨네요. SELECT SUBSTR(x.leave_from_date, 1, 4) AS leaveFromDate , b.description AS deptDesc , x.emp_no AS empNo , a.user_name AS empName , NVL(a.basic_leave, 0) AS leave_total , SUM(x.leave_days) AS leave_real , NVL(a.basic_leave, 0) - NVL( SUM(CASE WHEN x.leave_status = 'C' AND x.leave_type NOT IN ('05','06') THEN x.leave_days END) , 0) AS leftDays FROM et_leave x , t4users a , t4dept b WHERE a.user_id = x.emp_no AND b.dept_no = x.dept_no GROUP BY SUBSTR(x.leave_from_date, 1, 4) , x.emp_no , x.dept_no , a.user_name , b.description , a.basic_leave ORDER BY leaveFromDate, empNo ;