Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

merge query Expand / Collapse
Author
Message
Posted Monday, June 17, 2013 1:46 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Tuesday, May 6, 2014 12:05 AM
Points: 99, Visits: 304
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
Post #1464015
Posted Monday, June 17, 2013 3:19 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, July 24, 2014 9:56 AM
Points: 7,120, Visits: 13,491
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



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Post #1464038
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse