0

The Original Query:

7/20/2011   I
7/20/2011   O
7/19/2011   I
7/18/2011   O
7/17/2011   I

I = Check In while O = check Out

then I make a query to separate the I And O so It will return like this

select 
    case when CHECKTYPE='I' then checktime else 0 end as 'Check in',
    case when CHECKTYPE='O' then checktime else 0 end as 'Check Out',
    CHECKTYPE,BadgeNo,EmpName 
from 
    Checkinout 
where 
     and checktime BETWEEN '07/17/2011' and '07/26/2011' and EmpName = 'ASIDOY,' and BadgeNo <> '1900-01-01 00:00:00.000'
group by 
    checktime,CHECKTYPE,BadgeNo,EmpName 

Result

Check IN    Check out   Status
7/17/2011   1/1/1900    I
1/1/1900    7/18/2011   O
7/19/2011   1/1/1900    I
1/1/1900    7/20/2011   O
7/20/2011   1/1/1900    I

But I have one problem the query create puts (1/1/1900) in order to fill all blanks and i want to delete it. But I dont know how to write the query for it, in order to return like this.

Check In    Check Out
7/17/2011   7/18/2011
7/19/2011   7/20/2011
7/20/2011   7/21/2011
3
  • post the queries that you are using, that will help people to give you solution.
    – sudmong
    Jul 22, 2011 at 6:13
  • You need to include some ddl (data definition lang) and sample data - it looks you need a self-join, but i can't say on what.
    – jmoreno
    Jul 22, 2011 at 6:22
  • Where does the magical check out time come from in the third row of your desired result? Jul 22, 2011 at 7:50

2 Answers 2

2

try the following query

select 
    case when CHECKTYPE='I' then checktime end as Check in,
    case when CHECKTYPE='O' then checktime end as Check Out,
    CHECKTYPE,BadgeNo,EmpName 
from 
    Checkinout 

it will return NULL for empty columns

1
  • nope it will return NULL.. instead of 1/1/1900.. it will return NULL.. next question How to delete NULL so my query will 7/18/2011 move UP
    – mapet
    Jul 22, 2011 at 6:34
0

Assuming that there's always a check in before a "matching" check out, and not knowing how to deal with the missing check out for the third line of your result yet...

You need to join together the rows which you wish to contribute column values to the final output. It's going to be something like this:

SELECT
     ci.checktime,
     co.checktime
FROM
     CheckInOut ci
         inner join
     CheckInOut co
         on
             ci.checktime < co.checktime
         left join
     CheckInOut co_anti
         on
             ci.checktime < co_anti.checktime and
             co_anti.checktime < co.checktime and
             co_anti.checktype = 'o'
WHERE
    ci.checktype = 'i' and
    co.checktype = 'o' and
    co_anti.checktime is null

We're joining the table to itself 3 times in order to ensure that the row from co that matches against the row from ci is the earliest such row that could match.

I haven't included the other columns or conditions at this point, because I just wanted to focus on what's required to get the columns shown in your desired result. You can hopefully adapt the above to fit your other requirements.

1
  • i try this query but the checkout is correct and check_In is wrong
    – mapet
    Jul 22, 2011 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.