tags:

views:

82

answers:

4

alt text how can i write a query for this? I'm not an expert to write this much of complex queries in sql. I forgot to mention bid 3, 4 here

Thank you, Jestges

+2  A: 
SELECT
   a.bid, a.time checkin, ISNULL(b.time, '') checkout
FROM
   myTable a
LEFT JOIN
   myTable b ON a.bid = b.bid AND b.type = 'Check Out'
WHERE
   a.type = 'Check In'
ORDER BY
   a.time

EDIT

In response to your comment, seeing as there are several records of the same bid, and you only want one record per bid in your output, you need to specify which records you want in your output. When there are more than one, how do you decide what value to pick? If you always want the earliest checkin and the latest checkout, you might do something like this:

SELECT
    a.bid,
    MIN(a.time) checkin,
    ISNULL((SELECT
              MAX(time)
           FROM
              myTable
           WHERE
              bid = a.bid
              AND type = 'Check Out'), '') checkout
FROM
    myTable a
WHERE
    a.type = 'Check In'
GROUP BY
    a.bid

If that's not exactly what you want; adjust the use of MIN and MAX to fit your needs.

David Hedlund
Hi David thank you for your quick response. But when I try to execute the query I'm getting repeatative rows. But I need only 1 row per one day (Means datetime)
Nagu
I think you're still missing the min/maxing of times and a group by a.bid to eliminate the cases like bid=6 having two check ins
fd
@fd: did you write that before or after my edit? there is a group clause, now... @nagu: is this better? otherwise, fd might be onto something. if both do the trick equally well, fd's code looks cleaner.
David Hedlund
Before the edit, when I wrote my answer based on your query
fd
Didn't you meant `IFNULL` instead of `ISNULL`?
Kuroki Kaze
`ISNULL` is the sql server equivalent of mysql's `IFNULL`. OP didn't specify environment.
David Hedlund
@Nagu: If you want only one row per day, why does the sample result in your question have 3 rows for the same day?
Craig Young
@craig young: i'm thinking he probably means one result per `bid` *and* day. and i guess i haven't quite accounted for that in my edited query either, i've just grouped by `bid`
David Hedlund
@David, yes quite possibly 1 per bid and day; but then which check/checkout does he want in each case? He really needs to fix up his question.
Craig Young
+1  A: 

This will find the minimum check in and check out times for each bid id.

select bid, 
(select min(time) from table b where a.bid = b.bid and type = "Check In") as CheckIn,
(select min(time) from table c where a.bid = c.bid and type = "Check Out") as CheckOut
from table c
group by c.bid
Mongus Pong
You can do the same without subqueries :)
Kuroki Kaze
Seems like a hidden RBAR, please read up on: http://www.sqlservercentral.com/articles/T-SQL/61539/
Brimstedt
I cant be bothered to register to read the article, but I get the jist!
Mongus Pong
A: 

I think you're looking for something like this:

SELECT 
    `bid`, 
    MAX(IF(`type` = 'Check In', `time`, NULL)) AS `CheckIn`, 
    MAX(IF(`type` = 'Check Out', `time`, NULL)) AS `CheckOut` 
FROM `yourtable` 
GROUP BY `bid`
Kuroki Kaze
+4  A: 

Based on David's query, eliminating duplicate bids and restricting by check type.

SELECT
   a.bid, min(a.time) checkin, ISNULL(min(b.time), '') checkout
FROM
   myTable a
LEFT JOIN
   myTable b ON a.bid = b.bid
WHERE
   a.type = "Check In"
   AND
   b.type = "Check Out"
GROUP BY
   a.bid
ORDER BY
   a.time
fd
Note, I'm assuming you want the first checkin and checkout, if there are duplicates. That seems to be what the result table implies.
fd
Hi thank you.. but here also it repeats the rows because time is different event the date is same. But I want to display only one row for entire date. I tried to convert datetime to only date. But it is not working for me
Nagu
I converted like this ORDER BY ( (SELECT CONVERT(VARCHAR(10),a.datetime,111)))But it is showing error
Nagu
Sorry I don't understand the description of the error, can you show an example of the problematic results?
fd
@Nagu: If you want to convert datetime to date, use the following: `CAST(FLOOR(CAST(DateCol AS float)) AS datetime)`
Craig Young