我有一张桌子,看起来像:

时间:2018-07-31 20:51:32

标签: ms-access unpivot

我有一个表

     Material   90    89     88    87    ….2     1     0
      123456    34    30     26    38     14    12     8
      123457    47    42     33    54     38    27    42

我想将其转换为

     Date        123456     123457              
    Date()-90       34         47
    Date()-89       30         42
    Date()-88       26         33
    Date()-87       38         54
       ….        …………        ………..
    Date()-2        14         38
    Date()-1        12         27
    Date()           8         42

我发现了这个,但是试图弄清楚如何使用它:

Private Sub Unpivot_Click()
    Dim x As Integer
    Dim columncount As Integer
    Dim setRST As DAO.Recordset
    Dim sqlstr As String
    Dim qdf As DAO.QueryDef
    Dim fld As DAO.Field

    Set setRST = CurrentDb.OpenRecordset("Select * from TheTable")
columncount = setRST.Fields.Count
    Set qdf = db.CreateQueryDef ("", "Insert Into TheDestination ([Template], [Row], 
[Column], [Result]) VALUES (@Template, @RowNumber, @ColumnNumber, @Result)")
Do While Not setRST.EOF
   qdf.Parameters("@Template") = setRST!Template
   qdf.Parameters("@RowNumber") = setRST!row
   For Each fld In setRST.Fields
       If IsNumeric(fld.Name) Then
            qdf.Parameters("@ColumnNumber") = fld.Name
            qdf.Parameters("@Result") = fld.Value
            qdf.Execute 
       End If
      Next fld
      setRST.MoveNext
   Loop

End Sub

到目前为止,我有:

Public Function Unpivot()

Dim x As Integer
Dim columncount As Integer
Dim setRST As DAO.Recordset
Dim sqlstr As String
Dim qdf As DAO.QueryDef
Dim fld As DAO.Field

Set setRST = CurrentDb.OpenRecordset("SSMaterialByDayQ322")
columncount = setRST.Fields.Count
Set qdf = db.CreateQueryDef("", "Insert INTO SSHistoryQ322 ([Date], [Row], [Column], [Result]) VALUES (@Template, @RowNumber, @ColumnNumber, @Result)")
Do While Not setRST.EOF
   qdf.Parameters("@Template") = setRST!Date
   qdf.Parameters("@RowNumber") = setRST!row
   For Each fld In setRST.Fields
       If IsNumeric(fld.Name) Then
            qdf.Parameters("@ColumnNumber") = fld.Name
            qdf.Parameters("@Result") = fld.Value
            qdf.Execute
       End If
   Next fld
   setRST.MoveNext
Loop

End Function

2 个答案:

答案 0 :(得分:0)

如果表中需要“取消透视”的字段不超过50个,则UNION查询可以将数据重新排列为规范化的结构。没有用于UNION的查询构建器或向导,必须在SQL视图中键入或复制/粘贴。 UNION最多只能有50条SELECT行。

SELECT "Date()-90" AS [Date], Material, [90] AS Data FROM SSMaterialByDayQ322
UNION SELECT "Date()-89", Material, [89] FROM SSMaterialByDayQ322
UNION SELECT "Date()-88", Material, [88] FROM SSMaterialByDayQ322
UNION SELECT "Date()-87", Material, [87] FROM SSMaterialByDayQ322
UNION SELECT "Date()-2", Material, [2] FROM SSMaterialByDayQ322
UNION SELECT "Date()-1", Material, [1] FROM SSMaterialByDayQ322
UNION SELECT "Date()-0", Material, [0] FROM SSMaterialByDayQ322;

如果字段超过50年,则VBA可以将记录写入表中,请考虑:

Public Sub Unpivot()
Dim x As Integer
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SSMaterialByDayQ322")
Do While Not rst.EOF
   For x = 1 To rst.Fields.Count - 1
        CurrentDb.Execute "INSERT INTO SSHistoryQ322([Date], Material, Data)" & _
                          "VALUES('Date()-" & rst.Fields(x).Name & "', " & rst!Material & ", " & rst.Fields(x) & ")"
   Next
   rst.MoveNext
Loop
End Sub

然后,只要唯一的Material值不超过254个,就可以从查询或表中构建CROSSTAB。

TRANSFORM First(Query1.Data) AS FirstOfData
SELECT Query1.Date
FROM Query1
GROUP BY Query1.Date
PIVOT Query1.Material;

建议不要将日期用作字段名称,因为它是保留字。

答案 1 :(得分:0)

您可以先进行联合查询

SELECT 'Date()-90' As [Date], Material, [90] As [Value] FROM TheTable
UNION ALL
SELECT 'Date()-89' As [Date], Material, [89] As [Value] FROM TheTable
UNION ALL
SELECT 'Date()-88' As [Date], Material, [88] As [Value] FROM TheTable
UNION ALL
SELECT 'Date()-87' As [Date], Material, [87] As [Value] FROM TheTable
...
UNION ALL
SELECT 'Date()-02' As [Date], Material, [2] As [Value] FROM TheTable
UNION ALL
SELECT 'Date()-01' As [Date], Material, [1] As [Value] FROM TheTable
UNION ALL
SELECT 'Date()-00' As [Date], Material, [0] As [Value] FROM TheTable

当然,您可以使用VBA循环创建此查询。将此SQL文本存储为查询qryFlatten

现在,我们从该查询构建透视查询。这就是我们的最终结果

TRANSFORM Sum(qryFlatten.Value) AS SumOfValue
SELECT qryFlatten.Date
FROM qryFlatten
GROUP BY qryFlatten.Date
ORDER BY qryFlatten.Date DESC 
PIVOT qryFlatten.Material;