将列数据转换为行数据

时间:2019-05-23 20:35:19

标签: ms-access

我的SQL能力非常有限,我只使用了简单的查询并加入了MS Access。我的任务是转换此数据:

Meter_Number    Date    Time        Kwh
93112575    23/05/2019  08:00:00    278.334
93112575    23/05/2019  09:00:00    279.254
93112575    23/05/2019  10:00:00    280.012
93112575    23/05/2019  11:00:00    281.315
93112575    23/05/2019  12:00:00    282.256
93112575    23/05/2019  13:00:00    285.852
18794160    22/05/2019  14:00:00    6145.504
18794160    22/05/2019  15:00:00    6147.968
18794160    22/05/2019  16:00:00    6150.855
18794160    22/05/2019  17:00:00    6155.283
18794160    22/05/2019  18:00:00    6161.977
18794160    22/05/2019  19:00:00    6162.854
18794160    22/05/2019  20:00:00    6163.644
18794160    22/05/2019  21:00:00    6165.763
18794160    22/05/2019  22:00:00    6169.031
18794160    22/05/2019  23:00:00    6172.302
18794160    23/05/2019  00:00:00    6175.58
18794160    23/05/2019  01:00:00    6178.863
18794160    23/05/2019  02:00:00    6182.146
18794160    23/05/2019  03:00:00    6185.426
18794160    23/05/2019  04:00:00    6188.709
18794160    23/05/2019  05:00:00    6191.994
18794160    23/05/2019  06:00:00    6195.276
18794160    23/05/2019  07:00:00    6197.547
18794160    23/05/2019  08:00:00    6198.336
18794160    23/05/2019  09:00:00    6199.126
18794160    23/05/2019  10:00:00    6200.301
18794160    23/05/2019  11:00:00    6208.569
15296497    22/05/2019  14:00:00    62.064
15296497    22/05/2019  15:00:00    62.095
15296497    22/05/2019  16:00:00    63.044
15296497    22/05/2019  17:00:00    64.062
15296497    22/05/2019  18:00:00    65.024
15296497    22/05/2019  19:00:00    65.085

转换为这种格式:

93112575,#1,23/05/2019,08:00:00,278.334,#2,23/05/2019,09:00:00,279.254,#3,23/05/2019,10:00:00,280.012 ~etc.
18794160,#1,22/05/2019,14:00:00,6145.504,#2,22/05/2019,15:00:00,6147.968,#3,22/05/2019,16:00:00,6150.855,#4,22/05/2019,17:00:00,6155.283 ~etc.
15296497,#1,22/05/2019,14:00:00,62.064,#2,15296497,22/05/2019,15:00:00,62.095,#3,22/05/2019,16:00:00,63.044

这是在您的网站上发布的类似问题: R Programming Converting data columns to Rows

任何帮助将不胜感激。我确实知道这不是一项简单的任务,并且如果没有人有时间协助我,也不会受到冒犯。

我从您的一个页面上复制了一些代码,并将其添加到事件过程中,它填充了表格,但将所有数据都放在一列中。

1 个答案:

答案 0 :(得分:0)

以下是使用代码重新格式化数据的示例:
首先,我将数据导入到访问表中:

enter image description here

然后我使用vba遍历表格:


Public Sub ReformatData()
'declarations
'Dim rstData2 As DAO.Recordset
'Set rstData2 = CurrentDb.OpenRecordset("tblData2", dbOpenTable)
Dim rstData As DAO.Recordset
Dim currentMeter_Number As Long
Dim counter As Integer
Dim recordstring As String
Set rstData = CurrentDb.OpenRecordset("tblData", dbOpenTable)
'loop through table
If Not rstData.EOF Then
rstData.MoveFirst
Do While Not rstData.EOF
counter = 1
'process first Meter_Number in set
currentMeter_Number = rstData!Meter_Number
recordstring = recordstring & currentMeter_Number & ",#1," & rstData!DateRead & "," & _
Format(rstData!TimeRead, "hh:mm:ss") & "," & rstData!Kwh
rstData.MoveNext
    'process the rest of set
    Do While Not rstData.EOF
    If Not currentMeter_Number = rstData!Meter_Number Then
    Exit Do
    Else
        If Not IsNull(rstData!Meter_Number) Then
        If currentMeter_Number = rstData!Meter_Number Then
        counter = counter + 1
        recordstring = recordstring & ",#" & counter & "," & rstData!DateRead & "," & _
        Format(rstData!TimeRead, "hh:mm:ss") & "," & rstData!Kwh
        rstData.MoveNext
        End If
        End If
        End If
    Loop
recordstring = Left(recordstring, Len(recordstring) - 1) 'remove last comma
Debug.Print recordstring & vbCrLf
'rstData2.AddNew
'rstData2!MeterString = recordstring
'rstData2.Update
recordstring = ""
Loop
End If
End Sub

目前尚不清楚要在哪里输出数据,因此我演示了输出以进行调试以及将其输出到另一个已经存在的表。表格版本给出:

enter image description here