Click here to Skip to main content
15,920,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have two rows corresponding to employee attendace in my table say Table1 as shown below. First row records the In Time and second row records the Out Time.
EmpId ,AttendanceTime
1 07:30
1 04:30
2 07:35
2 04:45

Now i want to read data from Table1 and store each employee data in the columns Empid,Intime,OutTime in single row of Table2. How to make sql query for this purpose
Posted
Comments
Andy Lanng 1-Sep-15 6:46am    
Are there any other reference points like table id or date?
xpertzgurtej 1-Sep-15 7:00am    
yes there is date column in both tables
Andy Lanng 1-Sep-15 7:04am    
how about an am / pm or is time stored as a dbType time?

basically, is there a column you can Order By to get the in / out in the correct order?
xpertzgurtej 1-Sep-15 7:16am    
yes time is stored as datetime
xpertzgurtej 1-Sep-15 7:40am    
there is also a column AttendanceType which stores 0 for intime and 1 for outime in Table1

i am not much clear but i think you need similar. this is just an example.
SQL
create table Attendance
(
	Emp varchar(10), 
	LoginDateTime datetime, 
	AttendanceType tinyint
)

insert into Attendance values ('John', GETDATE()-1,0)
insert into Attendance values ('John', GETDATE(), 1)

SELECT Emp as 'Employee', [0] as 'Intime', [1] as 'Outtime'
FROM (
    SELECT
    AttendanceType,
        LoginDateTime, Emp
    FROM Attendance
) as temp
PIVOT
(
    max(LoginDateTime)
    FOR AttendanceType in ([0], [1])
)AS pvoit
 
Share this answer
 
Hello,

You can try this sql query

SQL
SELECT EmpID AS EmployeeID
	, AttendanceTime AS InTime
	, (SELECT AttendanceTime FROM Employee WHERE EmpID = EmployeeID 
           AND InTime < AttendanceTime) AS OutTime
FROM Employee
GROUP BY EmpID;
 
Share this answer
 
Hope this helps

http://streetrat.in/Articles/Content/Sample-Sql-Server-Pivot-Query104.aspx?RatHistory=104[^]

This is similar to the context of your's.


insert into table2 (username, Intime, outtime)
select * from pvresult
--pivotquery result

hope u get this
 
Share this answer
 
v2
Comments
xpertzgurtej 8-Sep-15 4:53am    
Thanks..It helped me in getting my requirement done
Create Table2 with required column name and follow this code.

SQL
INSERT INTO table2 
SELECT * FROM table1;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900