Solved

SQL Query to un-normalize data

Posted on 2007-03-22
2
269 Views
Last Modified: 2008-02-01
Access 2003
Table
Name, Color
David, blue
David, red
David, yellow
Mike, red
Mike yellow
Mary, red

How do I make a query that will

Name, Color1, Color2, Color3, Color4
David, Blue, red, yellow
Mike, Red, Yellow
Mary, Red

This would be ok too
Name, Color1, Color2, Color3, Color4
David, Blue, red, yellow
Mike,, Red, Yellow
Mary, ,Red,

0
Comment
Question by:davidpm
[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
2 Comments
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 250 total points
ID: 18778444
use left outer join...

select a.name,a.color,b.color,c.color,d.color
from table as a
left outer join table as b
on a.name=b.name
and b.color>a.color
left outer join table as c
on a.name=c.name
and c.color>b.color
left outer join table as d
on a.name=d.name
and d.color>c.color
order by 1
0
 
LVL 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) earned 250 total points
ID: 18779176
we can use a crosstab query here.
from your data create a query eg.,

SELECT TableA.Name, TableA.Color, TableA.Color AS Col
FROM TableA;
save as Q2

*note
  we need the third column for the crosstab query

now create a crosstab query using Q2

TRANSFORM Last(Q2.Col) AS LastOfCol
SELECT Q2.Name
FROM Q2
GROUP BY Q2.Name
PIVOT IIF(Q2.Color="Red","Color1",IIF(Q2.Color="Blue","Color2" ,IIF(Q2.Color="Yellow","Color3","Color4")));

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 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