MS Access将行信息转换为列

时间:2010-07-12 22:56:08

标签: ms-access vba rows

我花了好几个小时试图解决这个问题。这是一个制造工厂。

工厂车间的人员使用计算机系统将他们的活动记录在不同的机器上,计算机系统产生如下数据:

Empnbr  Datestamp   Shift   Machnbr Time
00090   6/30/2010   1   0354    11:10
00090   6/30/2010   1   S       12:00
00098   6/30/2010   1   0920    7:00
00098   6/30/2010   1   0143    7:30
00098   6/30/2010   1   S       15:00
00101   6/30/2010   1   0920    7:00
00101   6/30/2010   1   0247    7:30
00101   6/30/2010   1   0147    7:40
00101   6/30/2010   1   S       15:00
00107   6/30/2010   1   0585    7:00
00107   6/30/2010   1   0143    12:00
00107   6/30/2010   1   S       15:00
00109   6/30/2010   1   0920    7:00
00109   6/30/2010   1   0154    7:30
00109   6/30/2010   1   0154    9:00
00109   6/30/2010   1   0154    9:43
00109   6/30/2010   1   0254    10:49
00109   6/30/2010   1   0154    12:30
00109   6/30/2010   1   S       15:00
00111   6/30/2010   1   0591    5:00
00111   6/30/2010   1   S       17:00
00114   6/30/2010   2   0585    15:00
00114   6/30/2010   2   S       23:00

我正在尝试编写的报告需要这种形式的信息

Empnbr      Datestamp      Shift      Time Start       Time End       Machine

这将允许我计算每人每天在每台机器上花费的时间。基本上,Access需要查看员工编号,日期戳和班次,并在第一次看到时进行。然后需要在下次看到时将该时间归因于第一个条目上的机器。这种情况一直持续到Access访问机器“S”,这意味着注销。

现在是时候采取一些Advil来解决这个问题了 - 谢谢你的帮助!

2 个答案:

答案 0 :(得分:0)

我会使用临时表。如果要显示报告,请使用VBA代码浏览输入表,构建报告数据并填充临时表。然后使用临时表中的数据显示报告。填充报告表的功能如下所示:

Public Sub BuildReportTable()
    Dim prevEmp As Integer
    Dim prevDate As Date
    Dim prevTime As Date
    Dim prevMachine As String
    Dim prevShift As Integer
    Dim rs As Recordset

    'Empty report table
    CurrentDb.Execute "delete from tblHourReport"

    'Open the data table
    Set rs = CurrentDb.OpenRecordset("tblHours", dbOpenDynaset)
    If Not rs.EOF Then rs.MoveFirst

    'Loop over each data record and create the report record
    prevEmp = 0
    Do While Not rs.EOF
        If prevEmp <> 0 And prevMachine <> "S" Then
            DoCmd.SetWarnings False
            DoCmd.RunSQL "insert into tblHourReport values (" _
                & prevEmp & ", '" _
                & prevDate & "', '" _
                & prevMachine & "', " _
                & prevShift & ", '" _
                & prevTime & "', '" _
                & rs!Time & "');"
            DoCmd.SetWarnings True
        End If
        prevDate = rs!Date
        prevShift = rs!shift
        prevEmp = rs!employee
        prevTime = rs!Time
        prevMachine = rs!machine
        rs.MoveNext
    Loop
End Sub

答案 1 :(得分:0)

为什么分组查询不适合?例如:

SELECT Empnbr, Datestamp, Shift, Machnbr, 
       Min(CDate([datestamp] & " " & [Time])) AS TimeStart, 
       Max(CDate([datestamp] & " " & [Time])) AS TimeEnd
FROM MachineLog 
GROUP BY Empnbr, Datestamp, Shift, Machnbr

编辑重新评论

也许:

SELECT m.Empnbr, m.Datestamp, m.Shift, m.Machnbr, 
     Nz((SELECT Max(CDate([datestamp] & " " & [Time])) 
         FROM MachineLog x WHERE x.Empnbr=m.Empnbr 
         AND x.Machnbr<m.Machnbr),CDate([datestamp] & " 08:00")) AS st, 
     Max(CDate([datestamp] & " " & [Time])) AS TimeEnd
FROM MachineLog m
GROUP BY m.Empnbr, m.Datestamp, m.Shift, m.Machnbr