안녕하세요. 아직도 해결이 안되서 다시한번 질문을 드려봅니다.
아래와 같이 통계자료를 스프레드에 출력하고자 합니다.
아래 쿼리로 적용했을시 에러가 납니다.
다른 비슷한 쿼리를 참고해서 해봤는데 잘안됩니다.
조건을 기간으로 9월1일부터 9월3일까지 준다고 가정했을때 쿼리문은 어떻게 되는지요.
소중한 답변 부탁드리겠습니다.
수고하세요~
9월1일 9월2일 9월3일
월요일 화요일 수요일
홍길동 출근 08:20 08:30 08:40
퇴근 18:00 19:00 19:30
김대한 출근 08:10 08:15 08:50
퇴근 19:00 20:50 16:00
김민국 출근 07:00 08:15 07:20
퇴근 19:20 21:00 16:50
DECLARE @cols AS NVARCHAR(MAX),@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ',' + QUOTENAME(cyymmdd) from koolance.dbo.TimeCheck FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
set @query = 'SELECT TimeCheck.cTime, UserName.UserID, UserName.USort, Code.Code, ' + @cols + '
from
(
SELECT TimeCheck.cTime, UserName.UserID, UserName.USort, Code.Code
FROM koolance.dbo.Code INNER JOIN
(
SELECT TimeCheck.cTime, UserName.UserID, UserName.USort, Code.Code, count(chhmmss) as total
from koolance.dbo.code
GROUP BY UserName.UserID, UserName.USort, TimeCheck.cTime, Code.Code
)s2
ON Code.CodeName = s2.cTime INNER JOIN
UserName ON TimeCheck.cUser = s2.UserID
) x
pivot
(
count(timecheck.chhmmss)
for timecheck.cyymmdd in (' + @cols + ')
) p '
execute(@query)
Comment 2
-
군고구마
2014.10.02 12:15
-
건우아빠
2014.10.04 22:35
withres as(select '홍길동' nm ,'20140901' cyymmdd , '08:20' s_ti ,'18:00' e_ti union allselect '홍길동' nm ,'20140902' cyymmdd , '08:30' s_dt ,'19:00' e_dt union allselect '홍길동' nm ,'20140903' cyymmdd , '08:40' s_dt ,'19:30' e_dt union allselect '김대한' nm ,'20140901' cyymmdd , '08:10' s_dt ,'19:00' e_dt union allselect '김대한' nm ,'20140902' cyymmdd , '08:15' s_dt ,'20:50' e_dt union allselect '김대한' nm ,'20140903' cyymmdd , '08:50' s_dt ,'16:00' e_dt union allselect '김민국' nm ,'20140901' cyymmdd , '07:00' s_dt ,'19:20' e_dt union allselect '김민국' nm ,'20140902' cyymmdd , '08:15' s_dt ,'21:00' e_dt union allselect '김민국' nm ,'20140903' cyymmdd , '07:20' s_dt ,'16:50' e_dt ) ,result as(select a.nm, a.cyymmdd, b.no, right(a.cyymmdd,2) dd, case when b.no = '1' then a.s_ti else a.e_ti end job_timefrom res a cross join ( select 1 no union all select 2 no ) bwhere a.cyymmdd between '20140901' and '20140930' )select nm, case when no = '1' then '출근' else '퇴근' end gb, max([01]) [01], max([02]) [02], max([03]) [03]from result apivot(max(job_time)for dd in ([01],[02],[03])) as pvtgroup by nm , noorder by nm
궁금한게 결과가 어떻게 나와야 하나요??