Transform and Pivot in Ms-Access

荒凉一梦 提交于 2019-12-13 08:29:26

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/48027329/transform-and-pivot-in-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!