Access Union/Pivot to Swap Columns and Rows

后端 未结 1 1326

Access 2010 here. I have a query (Thank you Andomar!):

SELECT Inspection.Date, count(*) AS [# Insp], sum(iif(Disposition = \'PASS\',1,0)) AS [# Passed], sum(iif(         


        
1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-24 04:47

    This will have to be a two-step process to transform. First you will have to rotate the data in your current query to be in rows instead of columns, then you will have to transform the dates into columns instead of rows.

    The query will be something like this:

    TRANSFORM max(val) as MaxValue
    SELECT col
    FROM
    (
      SELECT [Date], '# Insp' as Col, [# Insp] as  val 
      FROM yourQuery
      UNION ALL
      SELECT [Date], '# Passed' as Col, [# Passed] as val
      FROM yourQuery
      UNION ALL
      SELECT [Date], '# Failed' as Col, [# Failed] as val
      FROM yourQuery
      UNION ALL
      SELECT [Date], '% Acceptance' as Col, [% Acceptance] as val
      FROM yourQuery
    ) 
    GROUP BY col
    PIVOT [Date]
    

    I am guessing the your current query is saved in your database, you will replace the yourQuery in my example with the name of your query.

    I just tested this in MS Access 2003 with the values in your sample above and it produced the result you want.

    0 讨论(0)
提交回复
热议问题