2015-10-17 94 views
-2

我有一个表具有这种结构SQL服务器如何使集团内部的集团 - 使用GROUP BY

enter image description here

EmployeeID | LogDate | LogTime | TerminalID | InOut | Accepted 

此表处理所有员工的主治

Where InOut Column = 0 - > In(员工只需打开以启动他的 班次)

其中INOUT柱= 1 - >输出(该员工就切出要 结束晚班)

样数据时我做一个选择 enter image description here

例如前面图像有雇员= 1009 在2015-10-14(06:56:28)还有两拳(06:56:28)还有(16:13:51) 我需要在结果中给我他应该参加这两次的时间2次 这是(09: 17:23)

我想要这样的结果结构

EmployeeID | LogDate | NumberOfPunshings | State | HoursAttending 

我尝试这个查询

 select 
Employeeid as EmpID, 
logdate as logdt, 
count(accepted) NumberOfPunshings, 
case count(accepted)     WHEN 1 THEN 'No Out Punch' 
              WHEN 2 THEN 'Perfect' 
              Else 'Null' 
              End as State, 
CONVERT(varchar(12), 
     DATEADD(minute, 
     DATEDIFF(minute, 
     (select top 1 LogTime from Accesslog where Inout=0), 
     (select top 1 LogTime from Accesslog where InOut=1)), 0), 114) as HoursAttending 

from Accesslog 
where Logdate = CAST('2015-10-14' AS datetime) 
group by 
employeeid,logdate 
order by Employeeid 

但它不工作,因为我想这只是在HoursAttending

enter image description here

走(的表)一个第一值

我已经创建SQL小提琴来证明您的数据看起来

http://sqlfiddle.com/#!3/0152f9/1

EmpID |   logdt   | NumberOfPunshings | State | HoursAttending 

1009 | October, 14 2015 00:00:00 |   2   | Perfect | 08:00:00:000 
1088 | October, 15 2015 00:00:00 |   2   | Perfect | 08:00:00:000 

预期的结果必然是

EmpID |   logdt   | NumberOfPunshings | State | HoursAttending 

1009 | October, 14 2015 00:00:00 |   2   | Perfect | 08:00:00:000 
1088 | October, 15 2015 00:00:00 |   2   | Perfect | 06:00:00:000 

在这些表中HoursAttending柱因为

第一员工(1009)开始从08:00工作到16:00在的差异,因此 持续时间(08:00)是正确的。

第二名员工(1088)从009:00开始工作到15:00,因此 的持续时间必须是(06:00)而不是(08:00)正确。

+1

你所说的 '不工作' 是什么意思? –

+0

@vkp我的意思是只有HoursAttending列在每行中取一个值。我将更新结果图片的帖子 – Loai

+0

您在子查询 – Horaciux

回答

2

想象一下,如果我们可以轻松获得数据,我们就能清楚地理解预期的最终结果应该是什么样子。

SQL Fiddle

的MS SQL Server 2014架构设置

CREATE TABLE Accesslog 
    ([EmployeeID] int, [LogDate] datetime, [LogTime] datetime, [Terminal] varchar(11), [InOut] int, [Accepted] int) 
; 

INSERT INTO Accesslog 
    ([EmployeeID], [LogDate], [LogTime], [Terminal], [InOut], [Accepted]) 
VALUES 
    (1009, '2015-10-14 00:00:00', '1900-01-01 06:36:06', 'abcdedghijk', 0, 1), 
    (1009, '2015-10-14 00:00:00', '1900-01-01 16:22:41', 'abcdedghijk', 1, 1) 
; 

查询1

select 
     EmployeeID 
     , LogDate 
     , MIN(case when InOut = 0 then cast(LogTime as time) end) as LogTimeMin 
     , MAX(case when InOut = 1 then cast(LogTime as time) end) as LogTimeMax 
     , COUNT(*) as CountPunches 
     , CONVERT(varchar(12), 
     DATEADD(minute, 
     DATEDIFF(minute, 
     MIN(case when InOut = 0 then cast(LogTime as time) end), 
     MAX(case when InOut = 1 then cast(LogTime as time) end)), 0), 114) as HoursAttending 
from Accesslog 
group by 
     EmployeeID 
     , LogDate 

Results

| EmployeeID |   LogDate | LogTimeMin | LogTimeMax | CountPunches | HoursAttending | 
|------------|------------------|-------------|-------------|--------------|----------------| 
|  1009 | October, 14 2015 | 06:36:06.00 | 16:22:41.00 |   2 | 09:46:00:000 | 

PART 2

为了解决偏移的问题,请尝试以下方法。请注意,如果你再想每天只有一个排,使用一组通过查询很好,但做下面的查询看到成“派生表”(子查询)

SQL Fiddle

MS SQL服务器2014架构设置

CREATE TABLE Accesslog 
    ([EmployeeID] int, [LogDate] datetime, [LogTime] datetime, [Terminal] varchar(11), [InOut] int, [Accepted] int) 
; 

INSERT INTO Accesslog 
    ([EmployeeID], [LogDate], [LogTime], [Terminal], [InOut], [Accepted]) 
VALUES 
    (1209, '2015-10-14 00:00:00', '1900-01-01 07:00:00', 'abcdedghijk', 0, 1), 
    (1209, '2015-10-14 00:00:00', '1900-01-01 12:01:00', 'abcdedghijk', 1, 1), 
    (1209, '2015-10-14 00:00:00', '1900-01-01 15:00:00', 'abcdedghijk', 0, 1), 
    (1209, '2015-10-14 00:00:00', '1900-01-01 20:02:00', 'abcdedghijk', 1, 1), 
    (1009, '2015-10-14 00:00:00', '1900-01-01 08:00:00', 'abcdedghijk', 0, 1), 
    (1009, '2015-10-14 00:00:00', '1900-01-01 16:00:00', 'abcdedghijk', 1, 1), 
    (1088, '2015-10-15 00:00:00', '1900-01-01 09:00:00', 'aaaa', 0, 1), 
    (1088, '2015-10-15 00:00:00', '1900-01-01 15:00:00', 'aaaa', 1, 1) 
; 

查询1

/* 
including (07:00 - 12:00) shift two (15:00 - 20:00) 
*/ 

WITH CTE as (
     SELECT *, ROW_NUMBER() OVER(PARTITION BY EmployeeID, LogDate, InOut 
            ORDER BY LogTime ASC) AS shiftno 
     FROM Accesslog 
      ) 
SELECT 
     ins.EmployeeID 
     , ins.LogDate 
     , ins.LogTime as LogTimeIn 
     , outs.LogTime as LogTimeOut 
     , ins.Accepted + ISNULL(outs.Accepted,0) as CountPunches 
     , CONVERT(varchar(12), 
     DATEADD(minute, 
     DATEDIFF(minute, 
     ins.LogTime, 
     outs.LogTime), 0), 114) as HoursAttending 
FROM CTE AS ins 
INNER JOIN CTE AS outs ON ins.InOut = 0 AND outs.InOut = 1 
         AND ins.EmployeeID = outs.EmployeeID 
         AND ins.LogDate = outs.LogDate 
         AND ins.shiftno = outs.shiftno 

Results

| EmployeeID |     LogDate |     LogTimeIn |    LogTimeOut | CountPunches | HoursAttending | 
|------------|---------------------------|---------------------------|---------------------------|--------------|----------------| 
|  1009 | October, 14 2015 00:00:00 | January, 01 1900 08:00:00 | January, 01 1900 16:00:00 |   2 | 08:00:00:000 | 
|  1088 | October, 15 2015 00:00:00 | January, 01 1900 09:00:00 | January, 01 1900 15:00:00 |   2 | 06:00:00:000 | 
|  1209 | October, 14 2015 00:00:00 | January, 01 1900 07:00:00 | January, 01 1900 12:01:00 |   2 | 05:01:00:000 | 
|  1209 | October, 14 2015 00:00:00 | January, 01 1900 15:00:00 | January, 01 1900 20:02:00 |   2 | 05:02:00:000 | 
+0

谢谢我更新了这篇文章,这里是SQL小提琴http://sqlfiddle.com/#!3/0152f9/1 – Loai

+0

非常好。你提供了一些数据。谢谢。由于我没有提供预期的结果,我不知道我到目前为止所做的是否正确。所以,不要猜测你期望的是什么,我会停下来。 –

+0

谢谢,这就是我所需要的,现在如果你不介意,现在查询中有另一个问题 – Loai