Query Problem [message #361889] |
Fri, 28 November 2008 07:40  |
sweetkhaliq
Messages: 200 Registered: April 2006
|
Senior Member |

|
|
Dear Group Members
I am working on attendance form. Attendance of employees of every day is stored in this form.
I want to get the max time_in of an employee with time out is null. I have made this query.
But the result is not as i require.
select time_in from attendance
where time_in = (select max(time_in) from attendance
where emp_code = '001820')
and time_out is null;
suppose data is as under
emp_code time_in time_out
001820 25-nov-08 09:02 AM 25-nov-08 05:15 PM
001820 26-nov-08 09:04 AM
001820 27-nov-08 09:02 AM 25-nov-08 05:15 PM
001820 28-nov-08 09:05 AM
I have to make the query to get the last row.
Thanks & regards
[Updated on: Fri, 28 November 2008 07:55] Report message to a moderator
|
|
|
|
Re: Query Problem [message #361898 is a reply to message #361889] |
Fri, 28 November 2008 08:06   |
_jum
Messages: 577 Registered: February 2008
|
Senior Member |
|
|
The query should give the last row as result (as wanted).
WITH a AS
(
SELECT '001820' emp_code,
TO_DATE('25-nov-08 09:02 AM', 'DD-MON-YY HH:MI AM') time_in,
TO_DATE('25-nov-08 05:15 PM', 'DD-MON-YY HH:MI AM') time_out
FROM DUAL
UNION ALL
SELECT '001820', TO_DATE('26-nov-08 09:04 AM', 'DD-MON-YY HH:MI AM'),
NULL
FROM DUAL
UNION ALL
SELECT '001820', TO_DATE('27-nov-08 09:02 AM', 'DD-MON-YY HH:MI AM'),
TO_DATE('25-nov-08 05:15 PM', 'DD-MON-YY HH:MI AM')
FROM DUAL
UNION ALL
SELECT '001820', TO_DATE('28-nov-08 09:05 AM', 'DD-MON-YY HH:MI AM'),
NULL
FROM DUAL)
SELECT time_in
FROM a
WHERE time_in = (SELECT MAX(time_in)
FROM a
WHERE emp_code = '001820') AND time_out IS NULL;
time_in
28.11.2008 09:05:00
|
|
|
|
|
|
|
Re: Query Problem [message #361913 is a reply to message #361909] |
Fri, 28 November 2008 09:37   |
_jum
Messages: 577 Registered: February 2008
|
Senior Member |
|
|
@sweetkhaliq
I'd bet the posted statement should give the correct result with the posted data.
But You didn't post Your Oracle version, nor a test case, so we only can guess.
|
|
|
Re: Query Problem [message #361921 is a reply to message #361913] |
Fri, 28 November 2008 10:26  |
 |
Michel Cadot
Messages: 68737 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
You want the last row or just the last date?
Still: post a representative test case: create table and insert statements along with the result you want with these data.
Regards
Michel
|
|
|