Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i have one table diff contain two columns

Checkin
Checkout

i am writing this query to insert record from other table into checkin

SQL
INSERT   INTO diff(CHECKIN)  SELECT checktime from checkinout where sensorid='1'

the result is like..

checkin.............checkout
------- --------
8/19/2013 14:53.....Null
8/19/2013 12:50.....Null

then i am executing the other query..
SQL
INSERT   INTO diff(CHECKOUT)  SELECT checktime from checkinout where sensorid='2'

the result look like this..

VB
checkin.............checkout
-------             --------
8/19/2013 14:53.....Null
8/19/2013 12:50.....Null
Null................8/19/2013 12:50
Null................8/19/2013 1:50

but i want to be display like

VB
8/19/2013 14:53.....8/19/2013 12:50
8/19/2013 12:50.....8/19/2013 12:50
Posted
Updated 25-Aug-13 23:21pm
v2
Comments
TrushnaK 26-Aug-13 1:53am    
Why dont you update your data rather than insert?
ArunRajendra 26-Aug-13 1:55am    
Do you have a primary / unique key field?
Nawab Ahmad 26-Aug-13 2:48am    
yes i have name ID..
Herman<T>.Instance 26-Aug-13 5:22am    
I see you have at least 4 questions about checkin and checkout table. Is SQL that hard for you?

I see either a design flaw or incomplete information in this scenario. Anyway one solution could be

SQL
select A.checkin, B.checkout
FROM
(
select ROW_NUMBER() ID, checkin
FROM diff
where checkout is null
) A INNER JOIN 
(
select ROW_NUMBER() ID, checkout
from diff
where checkin is null
) B ON A.ID = B.ID
 
Share this answer
 
Comments
Nawab Ahmad 26-Aug-13 2:16am    
Msg 1035, Level 15, State 10, Line 4
Incorrect syntax near 'ROW_NUMBER', expected 'OVER'.
Msg 1035, Level 15, State 10, Line 9
Incorrect syntax near 'ROW_NUMBER', expected 'OVER'.
Nawab Ahmad 26-Aug-13 2:53am    
I have a primary key called ID....
plz help
_Asif_ 26-Aug-13 4:09am    
Change the ID name to any other name and this should work. Please debug, it should not be hard!
To get your desired results you should use the UPDATE query; it will update the values in the selected column. While INSERT query adds a new row in the table due to which you are unable to get the results as per your expectations.
 
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