我有下表,其中包含所有人员的进出时间:
CREATE TABLE test (
timecardid INT
, trandate DATE
, employeeid INT
, trantime TIME
, Trantype VARCHAR(1)
, Projcode VARCHAR(3)
)
任务是获取所有带有 trantype A(可能使用 MIN)的最早 trantime 和带有 trantype Z(使用 Max)的最新 trantime,所有这些都在该 trandate 中(即 7 月 17 日的 trantype A 是 8:00 AM 和 trantype 7 月 17 日的 Z 是晚上 7:00)。
问题是,输出的格式应该与它来自的表的格式相同,这意味着我必须保留这些数据并过滤掉其余数据(不是该日期最早和最晚的输入/输出,每员工)
我目前的解决方案是使用两个不同的选择命令来获取所有最早的,然后获取所有最新的。然后将它们结合起来。
不过我想知道,有没有更简单的单字符串解决方案?
非常感谢。
编辑(我很抱歉,这里是示例。服务器是 SQL Server 2008):
Timecardid | Trandate | employeeid | trantime | trantype | Projcode
1 2013-04-01 1 8:00:00 A SAMPLE1
2 2013-04-01 1 9:00:00 A SAMPLE1
3 2013-04-01 2 7:00:00 A SAMPLE1
4 2013-04-01 2 6:59:59 A SAMPLE1
5 2013-04-01 1 17:00:00 Z SAMPLE1
6 2013-04-01 1 17:19:00 Z SAMPLE1
7 2013-04-01 2 17:00:00 Z SAMPLE1
8 2013-04-02 1 8:00:00 A SAMPLE1
9 2013-04-02 1 9:00:00 A SAMPLE1
10 2013-04-02 2 7:00:58 A SAMPLE1
11 2013-04-02 2 18:00:00 Z SAMPLE1
12 2013-04-02 2 18:00:01 Z SAMPLE1
13 2013-04-02 1 20:00:00 Z SAMPLE1
预期结果(在选择命令中,每个员工每天最早进出和最晚出出):
Timecardid | Trandate | employeeid | trantime | trantype | Projcode
1 2013-04-01 1 8:00:00 A SAMPLE1
4 2013-04-01 2 6:59:59 A SAMPLE1
6 2013-04-01 1 17:19:00 Z SAMPLE1
7 2013-04-01 2 17:00:00 Z SAMPLE1
8 2013-04-02 1 8:00:00 A SAMPLE1
10 2013-04-02 2 7:00:58 A SAMPLE1
12 2013-04-02 2 18:00:01 Z SAMPLE1
13 2013-04-02 1 20:00:00 Z SAMPLE1
非常感谢