1

我有一个看起来像这样的 Access 数据库:

ID     |   TestDate   |  Test1  |  Test2  |  Test 3  |
1      |    Date1     |   10    |    20   |    25    |
1      |    Date2     |   8     |    21   |    23    |
1      |    Date3     |   9     |    18   |    23    |
2      |    Date1     |   13    |    19   |    22    |

我想将行数据转换为列并保留先前列标题的名称,如下所示:

ID = 1
       |   Date1   |  Date2  |  Date3  |   etc...
Test1  |    10     |    8    |    3    |
Test2  |    20     |   21    |   18    |
Test3  |    25     |   23    |   23    |

ID = 2
       |   Date1   |
Test1  |    13     |
Test2  |    19     |
Test3  |    22     |


*The Date1 in the different IDs need not be the same. Date1 is the date the ID 
 had the Test for the first time. 

这样就更容易监控测试值的趋势。我尝试查找,但偶然发现的查询添加了值。我只需要转置数据而无需复制并粘贴到 Excel 中。非常感谢任何 MS Access 查询或 VBA 代码。谢谢你。

4

1 回答 1

1

怎么样:

TRANSFORM SUM(q.testval) AS sumoftestval
SELECT q.id,
       q.test
FROM   (SELECT t2.id,
               t2.testdate,
               "test1"  AS Test,
               t2.test1 AS TestVal
        FROM   t2
        UNION ALL
        SELECT t2.id,
               t2.testdate,
               "test2"  AS Test,
               t2.test2 AS TestVal
        FROM   t2
        UNION ALL
        SELECT t2.id,
               t2.testdate,
               "test3"  AS Test,
               t2.test3 AS TestVal
        FROM   t2) AS q
GROUP  BY q.id,
          q.test
PIVOT q.testdate; 

选择一个身份证

TRANSFORM Sum(q.testval) AS sumoftestval
SELECT q.test
FROM (SELECT t2.id,
               t2.testdate,
               "test1"  AS Test,
               t2.test1 AS TestVal
        FROM   t2
        UNION ALL
        SELECT t2.id,
               t2.testdate,
               "test2"  AS Test,
               t2.test2 AS TestVal
        FROM   t2
        UNION ALL
        SELECT t2.id,
               t2.testdate,
               "test3"  AS Test,
               t2.test3 AS TestVal
        FROM   t2)  AS q
WHERE q.id=1
GROUP BY q.test
PIVOT q.testdate;
于 2012-07-19T13:23:22.447 回答