Solved

SQL Query to un-normalize data

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

What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
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…

635 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