Click here to Skip to main content
15,919,341 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have one table difference

Checkin
Checkout

now i have to execute this two query ....

SQL
INSERT   INTO difference(CHECKIN) SELECT checktime from checkinout where sensorid='1'
INSERT   INTO difference(CHECKOUT) SELECT checktime from checkinout where sensorid='2'



After executing the first query record look like.

Checkin Checkout
--------- ---------
8/19/2013 14:53 Null
8/19/2013 16:46 Null


and when i execute the second querry it look like..
VB
Checkin       Checkout
 ---------       ---------
8/19/2013 14:53  Null
8/19/2013 16:46  Null
NULL             8/19/2013 15:53
Null             8/19/2013 16:53 


but i want it to be like this...

VB
Checkin       Checkout
 ---------       ---------
8/19/2013 14:53   8/19/2013 15:53
8/19/2013 16:46   8/19/2013 16:53
..




plz help...
Posted
Updated 25-Aug-13 23:19pm
v2

1 solution

check this example
SQL
DECLARE @checkinout TABLE(in_time DATETIME, out_time DATETIME, senerio_id INT, u_id INT)

INSERT INTO @checkinout
SELECT * FROM
(
    SELECT '2013-09-08 14:53' AS in_time, NULL AS out_time, 1 AS senerio_id, 1 AS u_id
    UNION ALL
    SELECT '2013-09-08 16:46' AS in_time, NULL AS out_time, 1 AS senerio_id, 2 AS u_id
    UNION ALL
    SELECT NULL AS in_time, '2013-09-08 15:53' AS out_time, 2 AS senerio_id, 1 AS u_id
    UNION ALL
    SELECT NULL AS in_time, '2013-09-08 16:53' AS out_time, 2 AS senerio_id, 2 AS u_id
) AS t

SELECT
    u_id,
    MAX(in_time) AS in_time,
    MAX(out_time) AS out_time,
    DATEDIFF(MI,MAX(in_time),
    MAX(out_time)) AS diffInMinute
FROM @checkinout
GROUP BY u_id

Happy Coding!
:)
 
Share this answer
 
Comments
Nawab Ahmad 26-Aug-13 3:28am    
Thanks for reply..

But i just want that checkout should come in the next row of checin
like..

checkin checkout
------ --------

8/19/2013 14:53 8/19/2013 15:53
Aarti Meswania 26-Aug-13 4:07am    
yes it's giving same o/p as you described in quastion

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