Note that there are some explanatory texts on larger screens.

plurals
  1. POsql server get min time?
    primarykey
    data
    text
    <p>Here the question,</p> <p>I have a table called <code>attendance</code> with 4 columns:</p> <pre><code>[Username] varchar(256) [Date] varchar(256) [Time] varchar(256) [Action] varchar(256) </code></pre> <p>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.</p> <p>Sample data like this:</p> <pre><code>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 </code></pre> <p>My expected result show below:</p> <pre><code>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 </code></pre> <p>I tried with this SQL statement:</p> <pre><code>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] </code></pre> <p>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.</p> <pre><code>Output: Username | Date | First In | First Out | Last In | Last Out User1 | 01/12/2012 | | | 18:44:17 | 22:05:31 </code></pre> <p>Is there any wrong with my SQL?</p> <p>Can anyone give any suggestion? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload