Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Pivot rows into columns

Pivot rows into columns

Pivot rows into columns

I have a sql table with these data
Year	Country		Ranking		Category
2018	Ghana		1		Swimming
2018	Sweden		2		Swimming
2018	Costa Rica	3		Swimming
2018	Jordan		1		Sprint
2018	Thailand	2		Sprint
2018	Finland		3		Sprint
2018	Myanmar		1		Boxing
2018	Peru		2		Boxing
2018	Belgium		3		Boxing
2017	Nigeria		1		Swimming
2017	Philippines	2		Swimming
2017	Haiti		3		Swimming
2017	Netherlands	1		Sprint
2017	Macedonia	2		Sprint
2017	Kuwait		3		Sprint
2017	Malaysia	1		Boxing
2017	New Zealand	2		Boxing
2017	Palau		3		Boxing

and would like to create a report that looks like this:

Category	Ranking	2017		2018
Swimming	1	Nigeria		Ghana
Swimming	2	Philippines	Sweden
Swimming	3	Haiti		Costa Rica
Sprint		1	Netherlands	Jordan
Sprint		2	Macedonia	Thailand
Sprint		3	Kuwait		Finland
Boxing		1	Malaysia	Myanmar
Boxing		2	New Zealand	Peru
Boxing		3	Palau		Belgium

I have googled examples of pivot, but all had to aggregate a column which I do not see being applicable in my case.
Any help will be greatly appreciated.

RE: Pivot rows into columns

One way to do this doesn't involve PIVOT and works with your limited data set. There are some assumptions regarding the full set of results for each year for each category.


SELECT A.Category , A.Ranking, A.Country [2017], B.country [2018]
FROM MalayGal A JOIN malaygal B
 ON A.Category = B.Category 
 AND A.Ranking = B.Ranking 
 AND A.year = 2017
 AND B.Year = 2018 

Hook'D on Access
MS Access MVP 2001-2016

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close