?
Solved

SQL Query to un-normalize data

Posted on 2007-03-22
2
Medium Priority
?
280 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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

719 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