简体   繁体   English

如何获得员工每个日期的最小和最大日期?

[英]How can I get the min and max date per date of employees?

This is my table looked like: 这是我的表,看起来像:

Employee_Number       |       DateTime
00000001              |       2014/01/14  09:20
00000001              |       2014/01/14  12:35
00000001              |       2014/01/14  13:35
00000002              |       2014/01/14  09:20
00000001              |       2014/01/14  22:49
00000001              |       2014/01/15  09:35
00000001              |       2014/01/15  10:35
00000001              |       2014/01/15  17:35
00000002              |       2014/01/14  12:34
00000002              |       2014/01/14  17:34

I want to do a select statement where I will get the min and the max datetime of employee per day something like this: 我想做一条选择语句,我将获得每天员工的最小和最大日期时间,如下所示:

Employee_Number       |       DateTime             MIN                  MAX
00000001              |       2014/01/14      2014/01/14  09:20    2014/01/14 22:49
00000001              |       2014/01/15      2014/01/15  09:35    2014/01/15 17:35
00000002              |       2014/01/14      2014/01/14  09:20    2014/01/14  17:34

I already searched google to find answer to my dilemma but the very near sql statement that I can have is this: 我已经搜索了谷歌以找到解决我难题的答案,但是我可以拥有的非常接近的sql语句是这样的:

declare @tmp table (
tranDate int,
tranTime datetime

                   )

 insert into @tmp
 select Convert(int, convert(nvarchar(100), DateTime,112)) ,DateTime from tblExtract

 select tranDate, min(tranTime) as 'min' , max(tranTime) as 'max' from @tmp
 group by tranDate

The problem is it only shows the min and max of the day not per employee_number. 问题是,它仅显示每天的最小值和最大值,而不是每个employee_number。 How can I solve this? 我该如何解决?

try this, assuming that the DateTime column is not stored as a string 假设DateTime列未存储为字符串,请尝试此操作

select Employee_Number, Cast([DateTime] as Date) as 'DateTime', MIN([DateTime]) as 'MIN', MAX([DateTime]) as 'MAX' 
from Employee_Table
group by Employee_Number, Cast([DateTime] as Date)
Select DateAdd(d, 0, DateDiff(d, 0, DateTime)) tranDate, Employee_Number, min(DateTime), max(DateTime)
From tblExtract
Group By 
DateAdd(d, 0, DateDiff(d, 0, DateTime)), Employee_Number

In MSSQL: 在MSSQL中:

 select 
    emloyee_id,
    convert(date,datetime) as Date_time
    min(datetime) as Min_date,
    max(datetime) as Max_date  
 from tblEmployee
 group by emloyee_id,convert(date,datetime)

group by based on employee_id and datetime (extracting only date part) will give us one row per combination of employee_id and date ,then we can select min and max of each group 根据employee_id和datetime(仅提取日期部分)进行分组,将根据employee_id和date的组合给我们一行,然后我们可以选择每个组的最小值和最大值

First of all, you must add Employee_Number to the tmp table. 首先,必须将Employee_Number添加到tmp表。

Next, In the last statement, you must write 接下来,在最后一条语句中,您必须编写

group by tranDate, Employee_Number

The reason is that you will get the minimum corresponding to each tranDate and Employee_Number pair, as required. 原因是您将根据需要获得与每个tranDate和Employee_Number对相对应的最小值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM