Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

vba - Transform and Pivot in Ms-Access

I have a table looks like:

Field1    Field2     Blank    Fail    Pass    Date
a         1          0        20      40      170101
a         2          1        19      99      170101
b         1          0        54      24      170101
c         3          1        24      30      170101
a         1          0        11      19      170102
b         2          0        21      266     170102
a         1          2        10      40      170103
....

The user then chooses the date range they want to display and I want my result to look like:

Field1  Field2 0101_B  0101_F  0101_P 0102_B  0102_F  0102_P 0103_B  0103_F  0103_P  
a       1      0       20      40     0       11      19     2       10      40
a       2      1       19      99
b       1      0       54      24
c       3      1       24      30      
b       2                             0       21      266

Any help really appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Options:

  1. a UNION query as the source for a CROSSTAB - Parameters really should reference controls on a form.
    PARAMETERS StartDate Long, EndDate Long; TRANSFORM Sum(Q.Data) AS SumOfData SELECT Q.Field1, Q.Field2, Q.Yr FROM (SELECT Field1, Field2, Blank AS Data, "B" AS Source, [Date], Mid([Date],3) & "_B" AS MonDayCat, Int(Left([Date],2)) AS Yr FROM Table1 UNION SELECT Field1, Field2, Pass, "P", [Date], Mid([Date],3) & "_P", Int(Left([Date],2)) AS Yr FROM Table1 UNION SELECT Field1, Field2, Fail, "F", [Date], Mid([Date],3) & "_F", Int(Left([Date],2)) AS Yr FROM Table1) AS Q WHERE (((Q.Date) Between [StartDate] And [EndDate])) GROUP BY Q.Field1, Q.Field2, Q.Yr PIVOT Q.MonDayCat;

  2. three CROSSTAB queries then join the CROSSTABS

  3. review http://allenbrowne.com/ser-67.html#MultipleValues

  4. VBA procedure writing records to a 'temp' table

None of these options will allow a full year of data if there is data for every day - 2.5 months at most. Building a stable report to run perpetually based on CROSSTAB is not easy. Review http://allenbrowne.com/ser-67.html#ColHead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...