简体   繁体   English

Access 2016 中的转换和旋转

[英]Transform and Pivot in Access 2016

I know there are topics on this, but the examples all go backwards from what I need to accomplish.我知道有关于此的主题,但这些示例都与我需要完成的工作背道而驰。

I have data in Access 2016 that looks like this:我在 Access 2016 中有如下所示的数据:

源数据

And I need it turned vertically so it looks like this:我需要它垂直转动,所以它看起来像这样:

首选输出

Etc. The student ID column never changes, but the number of questions does so I think it would have to be some kind of loop until end of record.等等。学生 ID 列永远不会改变,但问题的数量确实如此,我认为它必须是某种循环,直到记录结束。

Powerquery does this easily in excel, but I need it to be native in access. Powerquery 在 excel 中很容易做到这一点,但我需要它是本地访问。

Alternatively, if someone can explain how to do this TRANSFORM and PIVOT in Access 2013 SQL backwards I may be able to finish it from there.或者,如果有人可以向后解释如何在 Access 2013 SQL 中执行此TRANSFORM 和 PIVOT,我也许可以从那里完成它。

Normal pivot and unpivot won't handle this because I need to bring the column title down as a data field AND I need it to loop this move until the end of the record and won't know exactly how many columns there will be each time.正常的数据透视和反数据透视不会处理这个问题,因为我需要将列标题作为数据字段向下移动,并且我需要它循环移动直到记录结束,并且不知道每次将有多少列. So one time the file may go to Q07, and another go to Q43.所以一次文件可能会转到 Q07,另一次会转到 Q43。

My skill level with access is amateur.我的访问技能水平是业余的。 I can do enough VBA to copy and modify code but not enough to write this in Access.我可以做足够的 VBA 来复制和修改代码,但不足以在 Access 中编写它。

Thank you for your assistance!谢谢您的帮助!

You simply use union all :您只需使用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
. . .

First I tried this.首先我尝试了这个。

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;

But I couldn't figure out how to do it dynamically for a random number of answers.但我无法弄清楚如何为随机数量的答案动态地做到这一点。 So I blew the dust off my VB books and ended up with this which I'm sure is poorly coded and will make folks wince.所以我吹掉了我的 VB 书籍上的灰尘,最终得到了这个,我确信它的编码很差,会让人们畏缩。 But does work.但确实有效。

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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM