Solved

SQL Query to un-normalize data

Posted on 2007-03-22
2
267 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

828 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