Transpose a set of values in rows to columns in Access 2010

前端 未结 1 1327
予麋鹿
予麋鹿 2020-12-21 21:38

I have an Access database that looks something like this:

ID     |   TestDate   |  Test1  |  Test2  |  Test 3  |
1      |    Date1     |   10    |    20   |          


        
相关标签:
1条回答
  • 2020-12-21 22:03

    How about:

    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; 
    

    Select an ID

    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;
    
    0 讨论(0)
提交回复
热议问题