Access 2016 中的转换和透视
Posted
技术标签:
【中文标题】Access 2016 中的转换和透视【英文标题】:Transform and Pivot in Access 2016 【发布时间】:2018-10-29 14:52:53 【问题描述】:我知道有这方面的主题,但这些例子都与我需要完成的事情相反。
我在 Access 2016 中有如下所示的数据:
我需要它垂直转动,所以它看起来像这样:
等等。学生 ID 列永远不会改变,但问题的数量会改变,所以我认为它必须是某种循环,直到记录结束。
Powerquery 在 excel 中很容易做到这一点,但我需要它是本地访问。
或者,如果有人可以向后解释如何做到这一点TRANSFORM and PIVOT in Access 2013 SQL,我也许可以从那里完成它。
Normal pivot 和 unpivot 不会处理这个问题,因为我需要将列标题作为数据字段向下移动,并且我需要它循环这个移动直到记录结束并且不知道确切的多少每次都会有列。所以一次文件可能转到 Q07,另一次转到 Q43。
我的访问技能水平是业余的。我可以做足够多的 VBA 来复制和修改代码,但不足以在 Access 中编写。
感谢您的帮助!
【问题讨论】:
How to simulate UNPIVOT in Access 2010?的可能重复 PIVOT的反义词是UNPIVOT。 很好奇,表格最初是如何采用这种宽格式的? CSV? XML? Excel? 【参考方案1】:你只需使用union all
:
select studentId, 'Q01' as question, q01 as response
from data
union all
select studentId, 'Q02' as question, q02 as response
from data
union all
. . .
【讨论】:
这让我很接近,但我还需要将列标题作为一个字段向下移动。学生 ID / QuestionID / Response A000000 / Q01 / 1 A000000 / Q03 / 2 A000001 / Q01 / 3 等【参考方案2】:首先我尝试了这个。
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q01' as Question, [Q01] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q02' as Question, [Q02] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q03' as Question, [Q03] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q04' as Question, [Q04] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q05' as Question, [Q05] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q06' as Question, [Q06] as Response FROM Random_data_generator UNION ALL
SELECT StudentID, [Questionnaire #] as QuestionnaireID, 'Q07' as Question, [Q07] as Response FROM Random_data_generator
ORDER BY StudentID, Question;
但我不知道如何为随机数量的答案动态地做到这一点。所以我把我的 VB 书上的灰尘吹掉了,最后得到了这个,我确信它的代码很糟糕,会让人们畏缩。但确实有效。
Dim db As DAO.Database
Set db = CurrentDb
Dim qdf As DAO.QueryDef
ColumnCount = CurrentDb.TableDefs("Random_data_generator").Fields.Count
ColumnCount2 = ColumnCount - 2
Dim QueryString As String
Dim QueryEntry As String
Dim counter As Integer
Dim counterEntry As String
Dim QueryTest As Recordset
Dim QuestionnaireNum As String
counter = 1
Do While counter <= ColumnCount2
counterEntry = Format(counter, "00")
QueryString = "SELECT StudentID, [QuestionnaireNum] as QuestionnaireID, 'Q" & counterEntry & "' as Question, [Q" & counterEntry & "] as Response FROM Random_data_generator UNION ALL " & vbCrLf
counter = counter + 1
QueryEntry = QueryEntry + QueryString
Loop
counterEntry = Format(counter, "00")
QueryString = "SELECT StudentID, [QuestionnaireNum] as QuestionnaireID, 'Q" & counterEntry & "' as Question, [Q" & counterEntry & "] as Response FROM Random_data_generator ORDER BY StudentID, Question;"
QueryEntry = QueryEntry + QueryString
On Error Resume Next
DoCmd.DeleteObject acQuery, "tempQry"
On Error GoTo 0
Set qdf = db.CreateQueryDef("tempQry", QueryEntry)
DoCmd.OpenQuery ("tempQry")
【讨论】:
以上是关于Access 2016 中的转换和透视的主要内容,如果未能解决你的问题,请参考以下文章