MS Access 将行信息转换为列信息

发布于 2024-09-08 20:21:44 字数 1321 浏览 8 评论 0原文

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

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

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 来治疗头痛了——感谢您的帮助!

I've spent hours trying to figure this one out. This is for a manufacturing plant.

A person on the plant floor logs their activities on different machines using a computer system which produces data like this:

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

The report I'm trying to write needs the information in such a form

Empnbr      Datestamp      Shift      Time Start       Time End       Machine

This will allow me to calculate the amount of time spent on each machine per person per day. Basically, Access needs to see an employee number, datestamp, and shift and take the first time it sees. Then needs to take the next time it sees and attribute that time to the machine on the first entry. This continues to happen until Access comes across a machine "S" which means signout.

Now time to take some Advil for this headache -- thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怎言笑 2024-09-15 20:21:45

我会使用临时表。当您想要显示报告时,请使用 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

I would use a temporary table. When you want to display the report use VBA code to go through your input table, build the report data, and populate the temporary table. Then display your report using the data in the temporary table. The function to populate the reporting table would look something like this:

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
北城挽邺 2024-09-15 20:21:45

有什么理由不适合分组查询吗?例如:

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

Is there any reason why a grouping query would not suit? For example:

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

EDIT re comment

Perhaps:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文