SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


merge query


merge query

Author
Message
immaduddinahmed
immaduddinahmed
SSC-Enthusiastic
SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)SSC-Enthusiastic (153 reputation)

Group: General Forum Members
Points: 153 Visits: 420
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
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16174 Visits: 19543
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search