Solved

Crosstab or pivot table? I want the data not the sum!

Posted on 2006-11-09
15
365 Views
Last Modified: 2012-06-21
I currently have data in one format, and am not sure how to put it into another.

I have a table with records like this:

PupilID       Subject        Grade
1               English           A
2               English           B
3               English           A
4               English           C
1               Maths             D
2               Maths            E
3               Maths            A
4               Maths            A
1               French          B
2               French          B
3               French          C
4               French          A


What I want is a report / query that shows the data like so:

PupilID      English      Maths      French
1              A               D            B
2              B               E            B
3              A               A           C
4              C               A           A



I have tried pivot tables in Excel and Crosstabs in access to no avail.  They both want to perform a calculation (normally SUM) on the actual data (Grades).

Have I missed something obvious?  I have been puzzling over this for a while now.

Thanks in advance for any help with this one.

Richard.
0
Comment
Question by:highwaterhead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
15 Comments
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 17909641
Open up your crosstab in design view and change the sums shown in the total row to groupbys.
0
 
LVL 54

Expert Comment

by:nico5038
ID: 17909643
Put all fields in the graphical query editor. (PupilID       Subject        Grade)
Now change the querytype to crosstable (See Query menu)
There will appear a row with "GroupBy" there change the groupby under the Grade into MAX
Now set the PupilID to "Rowheader", the "Subject" to columnheader and the Grade to VAlue.

Run the query and check the outcome.

Nic;o)
0
 
LVL 44

Expert Comment

by:GRayL
ID: 17909787
You can use Max(), Min(), First(), Last() as there is only one number per Pivot
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 6

Accepted Solution

by:
gvlob earned 250 total points
ID: 17909813
Here you go, put this in a new query in SQL view (Note I gave the table name as grade, change that to your real table name):

TRANSFORM First(grade.Grade) AS FirstOfGrade
SELECT grade.PupilID
FROM grade
GROUP BY grade.PupilID
PIVOT grade.Subject;
0
 
LVL 6

Expert Comment

by:gvlob
ID: 17909941
Just like GRayL posted, my code uses the First().
0
 
LVL 44

Expert Comment

by:GRayL
ID: 17909996
For that matter you can continue to use Sum() - as there is only one number per Pivot
0
 
LVL 6

Expert Comment

by:gvlob
ID: 17910017
Wouldn't that give you an error since the data is text? I think you would get a type mismatch given the grades to be A, B, C, etc. The sum would work if it were a numeric value such as 4 (A), 3(B), etc.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 17910042
Of course you're right.  I was just seeing if you would pick up on my deliberate mistake!
0
 

Author Comment

by:highwaterhead
ID: 17910051
Thanks for replies.
Nico5038 - I have done as suggested, and I do get a cross tab query, but not how I described as wanted. Your solution produces multiple rows per pupilID.
What I want is only one row per PupilID. eg;

PupilID    English    Maths    French
1             A            D            B

What I get is:

PupilID    English    Maths    French
1             A                        
1                         D          
1                                      B


Any further advice?
0
 

Author Comment

by:highwaterhead
ID: 17910069
Actually you are right.  Working with Text values is problematic
0
 
LVL 6

Expert Comment

by:gvlob
ID: 17910085
Please try my query, it will work even with the letter grades.
0
 

Author Comment

by:highwaterhead
ID: 17910216
OK gvlob

I tried this with numerical data and it works a treat. I take it the "firstof" bit is the crucial bit.
I will test with text data in a moment.
Looking good.
0
 

Author Comment

by:highwaterhead
ID: 17910246
Great gvlob!
Mission accomplished.  Works with the text data as you predicted.
Thanks for help, will award points just now.

Richard.
0
 
LVL 54

Expert Comment

by:nico5038
ID: 17912176
Strange as the First() aggregate function is having a similar effect as the Max() I proposed.
Can you post the SQL you used so I can see what went wrong ?

Nic;o)
0
 
LVL 6

Expert Comment

by:gvlob
ID: 17914296
Highwaterhead, with numerical data you could use any of the functions given by GrayL, including the Sum.

Nico5038, I think that your solution should work also. From highwaterhead's remark it looks like he may have set the totals column for PupilID to something other than GroupBy. That's just my thinking though.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
run FileCopy code from a string 6 52
Win 10 Automation Error with .doCmd in Access 2 83
Combo box question 6 49
Ranking Students in Ms Access based on given Criteria 5 53
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question