;with cte as
(
select FingerId, [Date],
LogTime,
row_number() over(partition by FingerId order by LogTime) as rn
from InOut
)
select C3.EmployeeName,C1.FingerId,
C1.LogTime as InTime,
C2.LogTime as OutTime,
C2.[Date]
from cte as C1
left outer join cte as C2
on C1.FingerId = C2.FingerId and
C1.rn + 1 = C2.rn INNER JOIN
EmployeeMaster as C3 ON C3.Fingerid = C2.Fingerid
where C1.rn % 2 = 1 and C3.EmployeeName = 'xyz' and C1.[Date] between '2011-07-21' and '2011-07-29' order by C2.[Date]
select * From Inout order by LogTime asc
I am having INOUT Table It have 5 record and 3 record is of 2011-07-2011
InOuT Table:
AutoId FingerId LogTime Date
1 22 11:18:48 AM 2011-07-29
2 22 11:19:54 AM 2011-07-29
3 22 11:20:50 AM 2011-07-21
4 22 11:21:54 AM 2011-07-21
5 22 11:21:59 AM 2011-07-21
I am getting this output by this above query
EmployeeName FingerId InTime OutTime Date
xyz 22 11:20:50 AM 11:21:54 AM 2011-07-21
xyz 22 11:18:48 AM 11:19:54 AM 2011-07-29
I Want this type of OutPut:-
EmployeeName FingerId InTime OutTime Date
xyz 22 11:20:50 AM 11:21:54 AM 2011-07-21
xyz 22 11:21:59 AM ---- 2011-07-21
xyz 22 11:18:48 AM 11:19:54 AM 2011-07-29
Here 2nd row is having InTime and i want outtime should display "---" dash.But i m not getting with this query. Query is correct but need to do mod开发者_运维问答ify for this.
I've made some modifications to your query which I'm posting now with the changes highlighted in bold:
;WITH cte AS
(
SELECT FingerId, [Date],
LogTime,
ROW_NUMBER() OVER(PARTITION BY FingerId ORDER BY LogTime) AS rn
FROM InOut
)
SELECT C3.EmployeeName,C1.FingerId,
C1.LogTime AS InTime,
C2.LogTime AS OutTime,
COALESCE(C2.[Date], C1.[Date]) AS Date
FROM cte AS C1
LEFT OUTER JOIN cte AS C2 ON C1.FingerId = C2.FingerId AND C1.rn + 1 = C2.rn
INNER JOIN EmployeeMaster AS C3 ON C3.Fingerid = C1.Fingerid
WHERE C1.rn % 2 = 1
AND C3.EmployeeName = 'xyz'
AND C1.[Date] BETWEEN '2011-07-21' AND '2011-07-29'
ORDER BY C1.[Date]
Originally I was thinking about changing C1.[Date]
in the SELECT clause to C2.[Date]
, but then I wasn't sure it would be quite adequate a replacement in case the two dates were different. You can see for yourself which option is better, if any.
You'll have to use C1.[Date] instead of C2.[Date] in your WHERE clause - since you are outer joining to C2 and the row you are missing has a NULL value for C2.[Date].
精彩评论