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
834 views
in Technique[技术] by (71.8m points)

sql - Columns to Rows in MS Access

I am trying to create a query in MS Access to ultimately take the output from this:

Name        Cat 1   Cat 2   Cat 3   Cat 4   Cat 5   Cat 6
Joe         2       12      10      1       0       0
Bob         0       0       0       0       0       0
Jody        2       4       3       1       2       0
Harry       0       4       14      0       2       0

To something like this:

Name        Joe     Bob     Jody    Harry
Cat 1       2       0       2       0
Cat 2       12      0       4       4
Cat 3       10      0       3       14

Is this even possible?

EDIT

SELECT [Authorizer Name], 
Sum([Q1A - CD # 1]) AS [Category 1], 
Sum([Q2A- CD # 2A] + [Q8A- CD # 2A] + [Q10A- CD # 2A] 
    + [CTS A accurate- CD # 2A] 
    + [e-correspondence A accurate- CD # 2A]) AS [Category 2], 
Sum([Q7A- CD # 2B] + [Q9A- CD # 2B] + [Q11A- CD # 2B] 
    + [CTS A procedures- CD # 2B] 
    + [e-correspondence A procedures- CD # 2B]) AS [Category 3], 
Sum([Q4A- CD # 3]) AS [Category 4], 
Sum([Q5A- CD # 4]) AS [Category 5], Sum([Q12A- CD # 5]) AS [Category 6] 
FROM [Review Results] 
WHERE [Review Results].[Authorizer Name] = 1 
GROUP BY [Review Results].[Authorizer Name];

This is the way the table currently is:

Name    X1 X2A   X2B X2C X3A X3B X3C X4 X5 
Joe     1  5     0   1   1   5   6   0  0
Bob     2  7     0   2   1   4   2   1  9 
Billy   0  8     0   3   1   3   1   0  9 

This is what I would like to get to:

Name     Joe                  Bob    Billy 
X1       1                     2         0 
X2 (sum of X2A/X2B/X2C) 6      9         11 
X3 (sum of X3A/X3B/X3C) 12     7         5 
X4       0                     1         0 
X5       0                     9         9
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

First create a UNION query to normalize your table, then a crosstab to display the data:

SELECT [Name], "Cat 1" As Cat, [Cat 1] As CatVal FROM Table
UNION ALL
SELECT [Name], "Cat 2" As Cat, [Cat 2] As CatVal FROM Table
<...>

The query wizard will guide you through creating the crosstab.

EDIT re comments

To normalize the table of sample data, you can say:

SELECT [Name], "X1" As Cat, [X1] As CatVal FROM Table
UNION ALL
SELECT [Name], "X2" As Cat, Nz([X2A],0)+Nz([X2B],0)+Nz([X2C],0) As CatVal 
   FROM Table
UNION ALL
SELECT [Name], "X3" As Cat, Nz([X3A],0)+Nz([X3B],0)+Nz([X3C],0) As CatVal 
   FROM Table
UNION ALL
SELECT [Name], "X4" As Cat, [X4] As CatVal FROM Table
UNION ALL
SELECT [Name], "X5" As Cat, [X5] As CatVal FROM Table

Let us say the union query is QueryX:

TRANSFORM First(QueryX.CatVal) AS FirstOfCatVal
SELECT QueryX.Cat
FROM QueryX
GROUP BY QueryX.Cat
PIVOT QueryX.Name;

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

...