Click here to Skip to main content
15,916,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have got two queries for the same access 2010 database.
the first query is to retrieve the EmpID and InTime by using the where condition as InOut="0" and teh second query to retrieve the EmpID and the OutTime with condition Where InOut="1".
the query 1 as follows:(which is assigned to a string variable)

SQL
SELECT  Format(Min(Trans.Dt),"MM/dd/yyyy HH:mm:ss") AS InTime, Trans.EmpID FROM Trans WHERE (Format(Trans.Dt,"Short Date") in ('8/25/2014') and Trans.InOut="0") GROUP BY Format(Trans.Dt,"Short Date"), Trans.EmpID;


The query 2 as follows:(which is assigned to a string variable)
SQL
SELECT  Format(Max(Trans.Dt),"MM/dd/yyyy HH:mm:ss") AS OutTime, Trans.EmpID FROM Trans WHERE (Format(Trans.Dt,"Short Date") in ('8/25/2014') and Trans.InOut="1") GROUP BY Format(Trans.Dt,"Short Date"), Trans.EmpID;


now I have created two dbReader respectively as OLEDataReader for executing the 2 queries.
The I need a datatable that displays EmpID ,InTime (from dbReader1 ie.,query1) and OutTime(from dbReader2 ie.,Query2).

Thanks in advance!!!
Posted
Updated 5-Sep-14 2:18am
v2
Comments
MairajAhmed 5-Sep-14 8:16am    
You can temp table in sql and return combined data in one query.
_Asif_ 5-Sep-14 8:21am    
Why can't you tweak your SQL a bit and UNION both result? This way you have only one result which means one DataTable?
sai sruthi 5-Sep-14 8:28am    
I Don't Know how to use UNION with these two queries and can you please help me?
sai sruthi 5-Sep-14 8:35am    
The way OriginalGriff said was tried but the thing is I need InTime where InOut=0 and OutTime where InOut=1 but in that case I can give only one Where condition for InOut.
Sinisa Hajnal 5-Sep-14 8:37am    
What if you have two ins or two outs for the same empID? What should be the output?

The following gives you all the results with your in and out times in one query:

SELECT  Format(Min(Trans.Dt),"MM/dd/yyyy HH:mm:ss") AS InOutTime, Trans.EmpID FROM Trans WHERE (Format(Trans.Dt,"Short Date") in ('8/25/2014') and Trans.InOut="0") GROUP BY Format(Trans.Dt,"Short Date"), Trans.EmpID
UNION ALL
SELECT  Format(Max(Trans.Dt),"MM/dd/yyyy HH:mm:ss") AS InOutTime, Trans.EmpID FROM Trans WHERE (Format(Trans.Dt,"Short Date") in ('8/25/2014') and Trans.InOut="1") GROUP BY Format(Trans.Dt,"Short Date"), Trans.EmpID;


This one gives you the in and out time inseparate columns:

SELECT  Format(Min(Trans.Dt),"MM/dd/yyyy HH:mm:ss") AS InTime,'' AS OutTime Trans.EmpID FROM Trans WHERE (Format(Trans.Dt,"Short Date") in ('8/25/2014') and Trans.InOut="0") GROUP BY Format(Trans.Dt,"Short Date"), Trans.EmpID
UNION ALL
SELECT  Format(Max(Trans.Dt),'' As InTime, "MM/dd/yyyy HH:mm:ss") AS OutTime, Trans.EmpID FROM Trans WHERE (Format(Trans.Dt,"Short Date") in ('8/25/2014') and Trans.InOut="1") GROUP BY Format(Trans.Dt,"Short Date"), Trans.EmpID;
 
Share this answer
 
Comments
sai sruthi 5-Sep-14 9:29am    
Thanks PhilLenoir for giving me a reply but both the query returns only 2 columns such as InTime and EmpID but the InTime contains both InTime value and OutTime value.
Can you please give me a query that gives In and Out time separately?
PhilLenoir 5-Sep-14 9:40am    
The second of the two does so?
Sinisa Hajnal 7-Sep-14 8:30am    
But not in the same row for empID, you need outer select with min(inTime), max(outTime) with group by by empID
This should give you in and out times in single row for each EmpID

SQL
SELECT EmpID, MIN(InTime) as InTime, Max(OutTime) as OutTime
FROM
(
SELECT  Format(Min(Trans.Dt),"MM/dd/yyyy HH:mm:ss") AS InTime,'' AS OutTime Trans.EmpID FROM Trans WHERE (Format(Trans.Dt,"Short Date") in ('8/25/2014') and Trans.InOut="0") GROUP BY Format(Trans.Dt,"Short Date"), Trans.EmpID
UNION ALL
SELECT  Format(Max(Trans.Dt),'' As InTime, "MM/dd/yyyy HH:mm:ss") AS OutTime, Trans.EmpID FROM Trans WHERE (Format(Trans.Dt,"Short Date") in ('8/25/2014') and Trans.InOut="1") GROUP BY Format(Trans.Dt,"Short Date"), Trans.EmpID;
)
GROUP BY EmpID



If this solution solves your problem, please take some time to accept it so others can benefit. Thank you
 
Share this answer
 
v3
Try this

SELECT Format(Max(Trans.Dt),"MM/dd/yyyy HH:mm:ss") AS OutTime, Format(Min(Trans.Dt),"MM/dd/yyyy HH:mm:ss") AS InTime, Trans.EmpID FROM Trans  WHERE (Format(Trans.Dt,"Short Date") in ('8/25/2014') and (Trans.InOut="0" or Trans.InOut="1"))  GROUP BY Format(Trans.Dt,"Short Date"), Trans.EmpID;
 
Share this answer
 
v2
Comments
sai sruthi 8-Sep-14 0:58am    
It works but I am getting the intime and outtime as same but really thanks for your solution

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