Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9084787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:05:01+00:00 2026-06-16T21:05:01+00:00

Here the question, I have a table called attendance with 4 columns: [Username] varchar(256)

  • 0

Here the question,

I have a table called attendance with 4 columns:

[Username] varchar(256)
[Date] varchar(256)
[Time] varchar(256)
[Action] varchar(256)

What I wish to achieve is to get the first Check In and out time and Last Check In and out time. If there is only one Check In and out row in the table, it return the same.

Sample data like this:

Username | Date       | Time     | Action
--------------------------------------------
User1    | 01/12/2012 | 12:54:41 | Check In
User1    | 01/12/2012 | 18:26:36 | Check Out
User1    | 01/12/2012 | 18:44:17 | Check In
User1    | 01/12/2012 | 22:05:31 | Check Out

My expected result show below:

Output:
Username | Date       | First In | First Out | Last In  | Last Out
User1    | 01/12/2012 | 12:54:41 | 18:26:36  | 18:44:17 | 22:05:31

I tried with this SQL statement:

SELECT [USERNAME], [DATE]
, min(case when [action] = 'Clock In' then [time] else '' end) as 'First in'
, min(case when [action] = 'Clock Out' then [time] else '' end )as 'First out'
, max(case when [action] = 'Clock In' then [time] else '' end) as 'Last in'
, max(case when [action] = 'Clock Out' then [time] else '' end )as 'Last out'
FROM attendance
WHERE [USERNAME] = 'User1' AND [DATE] = '01/12/2012'
group by [username],[date]

It returns unexpected result that my min value always ” or empty but both my max value
‘Last in’ and ‘Last Out’ is in correct value.

Output:
Username | Date       | First In | First Out | Last In  | Last Out
User1    | 01/12/2012 |          |           | 18:44:17 | 22:05:31

Is there any wrong with my SQL?

Can anyone give any suggestion?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T21:05:03+00:00Added an answer on June 16, 2026 at 9:05 pm

    Try this:

    SELECT [USERNAME], [DATE]
    , min(case when [action] = 'Clock In' then [time]  end) as 'First in'
    , min(case when [action] = 'Clock Out' then [time] end )as 'First out'
    , max(case when [action] = 'Clock In' then [time] end) as 'Last in'
    , max(case when [action] = 'Clock Out' then [time] end )as 'Last out'
    FROM attendance
    WHERE [USERNAME] = 'User1' AND [DATE] = '01/12/2012'
    group by [username],[date]
    

    Are you understand where is the problem?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here's my question: I have a table TRUST(id INT, trustlevel INT) which contains records
Apologies for the vague question. Here it is: I have a table object created
I have a question. I have here 2 tables table 1 : products product_id
Here is a question that I have had for some time but never actually
First time asking a question here. I have two simple Javascript functions that 1.
I have a database with a table called categories. The table has 3 columns:
I have a table called emaildata consisting of 4 columns emailaddress, domainname, data and
Beginner question here, any help much appreciated. I have a table of users and
I have a table called Visits : ID(PKey,Int) PatiendID(int), DoctorID(int), ExpectedDate(date), ActualDate(date), How can
Following on from this question: Linq-to-SQL ToDictionary() I have a table called Domains ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.