1

I have this kind of database with columns: id, date, time, desc, user_id

id|date      |time    |desc|user_id
1 |12-11-1990|09:00:00|In  |1
2 |12-11-1990|18:00:00|Out |1
3 |12-11-1990|09:00:00|In  |2
4 |12-11-1990|18:00:00|Out |2
5 |13-11-1990|10:00:00|In  |1
6 |13-11-1990|17:00:00|Out |1

And I want to know, how to make the table looks like it like this when using query

date      |in      |out     |user_id
12-11-1990|09:00:00|18:00:00|1
12-11-1990|09:00:00|18:00:00|2
13-11-1990|10:00:00|17:00:00|1

Tried to subquery it, but get an error because it returns multiple rows.

SELECT date, (
    SELECT time 
    FROM absensi
    WHERE desc="In"
) AS CHECK_IN, (
    SELECT time 
    FROM absensi
    WHERE desc="Out"
) AS CHECK_OUT
FROM absensi GROUP BY date
4
  • Shown DATE values are not legal in MySQL, YYYY-MM-DD must be used. Commented Mar 29, 2021 at 8:45
  • 1
    Edit your question and show the results you want. Commented Mar 29, 2021 at 11:53
  • It's still unclear what output you want. Edit your question and show your expected output. Commented Mar 29, 2021 at 12:00
  • thank you for your advice, already edit the expected output. Commented Mar 29, 2021 at 23:52

2 Answers 2

2

I suspect you want one row per user per day. This might be as simple as:

select date, user_id, min(time), max(time)
from t
group by date, user_id;

This assumes that the in is always before the out.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response, I already edit the expected result for the question. What I want is show the check in and checkout time of each existing user everyday.
@AnandaPramono . . . I think this does what you want.
Thank you very much sir
0
SELECT dates.`date`, t1.`time` `In`, t2.`time` `Out`
FROM ( SELECT DISTINCT `date`
       FROM absensi ) dates
LEFT JOIN absensi t1 USING (`date`)
LEFT JOIN absensi t2 USING (`date`)
WHERE t1.desc = 'In'
  AND t2.desc = 'Out'

The query assumes that there is no 2 or more rows with the same (date, desc) combination.

1 Comment

Thank you for your response, But if there are more user, there will be duplicate rows with the same date and desc

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.