June 17, 2013 at 1:46 am
i want to merge this query
DECLARE @MinDate datetime,@MinDate datetime
SELECT @MinDate = MIN([Date]),
@MaxDate = MAX([Date])
FROM attend_log
SELECT p.Date,
q.TimeIn,
q.TimeOut,
q.shift,
p.eid
FROM
(
SELECT f.[Date],eid
FROM dbo.CalendarTable(@MinDate,@MaxDate,0,0) f
CROSS JOIN (SELECT DISTINCT eid FROM attend_log) t
)p
LEFT JOIN attend_log q
ON q.eid = p.eid
AND q.[Date] = p.[Date]
into this query
--drop table #temp1
select
[date],
min([Timein]) as First_Record,
sum(DATEDIFF(minute, [Timein], [Timeout])) as Time_Minutes
into #temp1 from Atend
where eid = 26446
group by [date]
GO
select
t.[date],
t.eid,
t.[Timein] as timein,
t.[Timeout] as timeout,
CONVERT(VARCHAR(8), DATEADD(minute, Time_Minutes, 0), 108) AS SpendTime,
CONVERT(VARCHAR(8), DATEADD(minute, ABS(540 - Time_Minutes), 0), 108) as excesshorttime,
case when (540 - Time_Minutes) > 0 then 'Short'
when (540 - Time_Minutes) < 0 then 'Excess'
else NULL end as ExcessShort,
case when (540 - Time_Minutes) >= 120 then 'HalfDay' else '' end as Remarks
FROM Atend t
left join #temp1 t2 on t.[date] = t2.[date] and t.[Timein] = t2.First_Record
where eid = 26446
order by t.[date], t.[Timein]
and show result like this
date-------------------------------eid------timein------------------------timeout-------------------spendtime--------excessshort
2013-01-04 00:00:00.000---26446--2013-06-12 09:29:00.000---2013-06-12 18:47:00.000---09:18:00--------00:18:00
2013-01-05 00:00:00.000---26446--2013-06-12 09:08:00.000---2013-06-12 13:34:00.000---07:41:00-------01:19:00
2013-01-06 00:00:00.000---26446-------------null---------------------null--------------------------null-----------------null
thanks for the help
immad
June 17, 2013 at 3:19 am
The result set from your second query already contains all of the columns in your requested output. What do you want to change?
Your first query is quite inefficient. The table attend_log is read three times. Try eliminating two of those reads. Something like this should do the trick:
SELECT
l.eid,
f.[Date],
l.TimeIn,
l.[TimeOut],
l.shift
FROM (
SELECT
eid,
[Date],
TimeIn,
[TimeOut],
shift,
MinDate = MIN([Date]) OVER (PARTITION BY (SELECT NULL)),
MaxDate = MAX([Date]) OVER (PARTITION BY (SELECT NULL))
FROM attend_log
) l
CROSS JOIN dbo.CalendarTable(l.MinDate,l.MaxDate,0,0) f
WHERE f.[Date] = l.[Date] OR l.[Date] IS NULL
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply