Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help about SELECT

Author  Topic 

rithython
Starting Member

1 Post

Posted - 2010-04-27 : 12:02:00
Anybody help me please?
I got a table tblAttendance like this

N EmpID Dates
1 1 27/04/2010 7:00:00 AM
2 1 27/04/2010 3:00:00 PM
3 2 27/04/2010 7:00:00 AM
4 2 27/04/2010 3:00:00 PM
5 2 28/04/2010 7:00:00 AM
6 2 28/04/2010 3:00:00 PM
7 2 28/04/2010 5:00:00 PM
8 2 28/04/2010 8:00:00 PM
11 2 29/04/2010 3:00:00 PM
12 2 30/04/2010 7:00:00 AM
13 2 30/04/2010 3:00:00 PM
9 3 27/04/2010 7:00:00 AM
10 3 27/04/2010 3:00:00 PM

I want to create select statement to have result as below:

EmpID In Out
1 27/04/2010 7:00:00 AM 27/04/2010 3:00:00 PM
2 27/04/2010 7:00:00 AM 27/04/2010 3:00:00 PM
2 28/04/2010 7:00:00 AM 28/04/2010 3:00:00 PM
2 28/04/2010 5:00:00 PM 28/04/2010 8:00:00 PM
3 27/04/2010 7:00:00 AM 27/04/2010 3:00:00 PM

Thanks you in advances.

Rithy

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-27 : 12:16:30
are you using SQL 2005?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-27 : 12:25:55
I think there's an error in your sample data, because there is no matching out time for N=11. Anyway, try this:
;WITH cte AS ( 
SELECT N, EmpID, Dates, ROW_NUMBER() OVER (PARTITION BY EmpID ORDER BY Dates) AS Row
FROM tblAttendance
), cte2 AS (
SELECT *, CASE WHEN (Row / 2) * 2 = Row THEN 1 ELSE 0 END AS Even
FROM cte
)

SELECT a.EmpID, a.Dates AS [In], b.Dates AS [Out]
FROM cte2 a
INNER JOIN cte2 b
ON a.EmpID = b.empID
AND a.Row = b.Row - 1
AND a.Even = 0


------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.

EDIT: N=11, not 8
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-27 : 12:33:39
quote:
Originally posted by visakh16

something like:-

SELECT t1.*,
CASE WHEN t2.[ time_stamp] IS NOT NULL THEN 1 ELSE 0 END,
t2.[ time_stamp]
FROM TableA t1
OUTER APPLY(SELECT [ time_stamp]
FROM TableB
WHERE [user]=t1.[user]
AND Ad = t1.Ad
AND [ time_stamp]> t1.[ time_stamp]
AND [ time_stamp] < DATEADD(mi,DATEDIFF(mi,0,t1.[ time_stamp])+1,0)
)t2


i cant understand on what basis you get rows for D though. they do not fall within 1 minute slot as in case of others

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Um... Wrong thread?

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-27 : 12:36:30
oops pasted on wrong one

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-27 : 12:40:32
why is 30 data not included in your output for 2?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -