im so confused on how to convert mssql server quries to laravel DB quries, i would be greatful if any one could show me how to achive this, please let me know if i could imporve it for usage with laravel.
thank you :)
IF OBJECT_ID('tempdb.dbo.#tempreport', 'U') IS NOT NULL
DROP TABLE #tempreport;
WITH CTE AS
(
SELECT
u.USERID,
u.TITLE,
u.NAME,
d.DEPTNAME,
DATEPART(dw, c.CHECKTIME) as day,
sc.lateminutes,
sc.earlyminutes,
sc.Workday as wd,
CAST(c.CHECKTIME as date) AS DT,
CONVERT(varchar(5), c.CHECKTIME, 108) AS CHECKTIME,
CONVERT(varchar(5), sc.StartTime, 108) as start,
CONVERT(varchar(5), sc.EndTime, 108) as eend,
CONVERT(varchar(5), sc.CheckInTime1, 108) +'-'+ CONVERT(varchar(5), sc.CheckInTime2, 108) as inn,
CONVERT(varchar(5), sc.CheckoutTime1, 108) +'-'+ CONVERT(varchar(5), sc.CheckoutTime2, 108) as oout,
CASE
WHEN
nrd.SDAYS = DATEPART(dw,c.checktime)
AND CAST(c.checktime as time) >= CAST(sc.CheckInTime1 as time)
AND nrd.SDAYS = DATEPART(dw,c.checktime)
AND CAST(c.checktime as time) <= CAST(sc.CheckInTime2 as time) THEN REPLACE(REPLACE(REPLACE(sc.Workday, '1' , 'min'), '2' , 'ain'), '3' , 'nin')
WHEN
nrd.SDAYS = DATEPART(dw,c.checktime)
AND CAST(c.checktime as time) >= CAST(sc.CheckOutTime1 as time)
AND nrd.SDAYS = DATEPART(dw,c.checktime)
AND CAST(c.checktime as time) <= CAST(sc.CheckOutTime2 as time) THEN REPLACE(REPLACE(REPLACE(sc.Workday, '1' , 'mout'), '2' , 'aout'), '3' , 'nout')
ELSE 'hide'
END AS TD
FROM CHECKINOUT c
FULL OUTER JOIN USERINFO u ON c.USERID = u.USERID
FULL OUTER JOIN DEPARTMENTS d ON d.DEPTID = u.DEFAULTDEPTID
FULL OUTER JOIN USER_OF_RUN uor ON u.userid = uor.userid
FULL OUTER JOIN NUM_RUN_DEIL nrd ON nrd.NUM_RUNID = uor.NUM_OF_RUN_ID
FULL OUTER JOIN SchClass sc ON sc.schClassid = nrd.SCHCLASSID
WHERE
u.USERID = 161 and
c.CHECKTIME between '2016-01-10' and '2016-05-11' and
DATEPART(dw, c.CHECKTIME) = nrd.SDAYS
),
fix as(
/**
* fix night shift out
* as the user will clock out after midnight
* minus a day from the DT and day to get right nout timestamp
*/
SELECT userid, name,deptname,checktime,lateminutes,earlyminutes,TD,
CASE WHEN TD ='nout' THEN DATEADD(day,-1,DT) ELSE DT END AS DT,
CASE WHEN TD ='nout' THEN day -1 ELSE day END AS day
FROM CTE
WHERE TD !='hide'
),
Overlaps as (
SELECT userid, name,deptname,lateminutes,earlyminutes,DT
,min,mout,ain,aout,nin,nout
FROM fix
PIVOT (Max(checktime)
FOR TD IN (
min,mout,ain,aout,nin,nout
)) AS pvt
)
select * INTO #tempreport
from overlaps
order by DT desc
select t.userid,t.name,t.deptname,t.dt
,(Case
When t.min IS NOT NULL THEN t.min
When t.min is null AND Exists(
SELECT nrd.SDAYS
FROM USER_OF_RUN uor
INNER JOIN NUM_RUN_DEIL nrd ON nrd.NUM_RUNID = uor.NUM_OF_RUN_ID
INNER JOIN SchClass sc ON sc.schClassid = nrd.SCHCLASSID
where uor.USERID = t.userid
AND nrd.SDAYS = DATEPART(dw,t.DT)
AND sc.WorkDay = '1'
)
Then 'N/A'
Else 'N/S'
End
) as min
,(Case
When t.mout IS NOT NULL THEN t.mout
When t.mout is null AND Exists(
SELECT nrd.SDAYS
FROM USER_OF_RUN uor
INNER JOIN NUM_RUN_DEIL nrd ON nrd.NUM_RUNID = uor.NUM_OF_RUN_ID
INNER JOIN SchClass sc ON sc.schClassid = nrd.SCHCLASSID
where uor.USERID = t.userid
AND nrd.SDAYS = DATEPART(dw,t.DT)
AND sc.WorkDay = '1'
)
Then 'N/A'
Else 'N/S'
End
) as mout
,(Case
When t.ain IS NOT NULL THEN t.ain
When t.ain is null AND Exists(
SELECT nrd.SDAYS
FROM USER_OF_RUN uor
INNER JOIN NUM_RUN_DEIL nrd ON nrd.NUM_RUNID = uor.NUM_OF_RUN_ID
INNER JOIN SchClass sc ON sc.schClassid = nrd.SCHCLASSID
where uor.USERID = t.userid
AND nrd.SDAYS = DATEPART(dw,t.DT)
AND sc.WorkDay = '2'
)
Then 'N/A'
Else 'N/S'
End
) as ain
,(Case
When t.aout IS NOT NULL THEN t.aout
When t.aout is null AND Exists(
SELECT nrd.SDAYS
FROM USER_OF_RUN uor
INNER JOIN NUM_RUN_DEIL nrd ON nrd.NUM_RUNID = uor.NUM_OF_RUN_ID
INNER JOIN SchClass sc ON sc.schClassid = nrd.SCHCLASSID
where uor.USERID = t.userid
AND nrd.SDAYS = DATEPART(dw,t.DT)
AND sc.WorkDay = '2'
)
Then 'N/A'
Else 'N/S'
End
) as aout
,(Case
When t.nin IS NOT NULL THEN t.nin
When t.nin is null AND Exists(
SELECT nrd.SDAYS
FROM USER_OF_RUN uor
INNER JOIN NUM_RUN_DEIL nrd ON nrd.NUM_RUNID = uor.NUM_OF_RUN_ID
INNER JOIN SchClass sc ON sc.schClassid = nrd.SCHCLASSID
where uor.USERID = t.userid
AND nrd.SDAYS = DATEPART(dw,t.DT)
AND sc.WorkDay = '3'
)
Then 'N/A'
Else 'N/S'
End
) as nin
,(Case
When t.nout IS NOT NULL THEN t.nout
When t.nout is null AND Exists(
SELECT nrd.SDAYS
FROM USER_OF_RUN uor
INNER JOIN NUM_RUN_DEIL nrd ON nrd.NUM_RUNID = uor.NUM_OF_RUN_ID
INNER JOIN SchClass sc ON sc.schClassid = nrd.SCHCLASSID
where uor.USERID = t.userid
AND nrd.SDAYS = DATEPART(dw,t.DT)
AND sc.WorkDay = '3'
)
Then 'N/A'
Else 'N/S'
End
) as nout
from #tempreport t
group by userid,name,deptname,dt,min,mout,ain,aout,nin,nout
order by t.dt DESC
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire