Dear Pommy:
I believe I worked with you earlier on this, in making the first
transformation. However, you have omitted the column I suggested,
which showed the ColorNumber giving the order in which they originally
occurred.
If you retain this value, or if you choose to order them
alphabetically, you can generate a ranking of them. Either way, this
can provide a way to sequentially produce a "crosstab like" series of
sets for the 20 repetitive sets of columns.
While this technique would be useful in producing a report, it is
seriously discouraged as a table structure. The table structure of
Range/ColorNumber/Color/Size/Quantity should be used for your storage
in any case. When you need to see the data in the other form, with
repetitive sets of columns, you can produce that from the data anytime
it is needed. But it should not be stored that way.
To create the form you asked about, here's a start.
SELECT range,
(SELECT colour FROM YourTable T1
WHERE T1.range = T.range
AND (SELECT COUNT(*) FROM YourTable T2
WHERE T2.range = T1.range AND T2.colour < T1.colour) = 0)
AS colour1,
(SELECT size FROM YourTable T1
WHERE T1.range = T.range
AND (SELECT COUNT(*) FROM YourTable T2
WHERE T2.range = T1.range AND T2.colour < T1.colour) = 0)
AS size1,
(SELECT quantity FROM YourTable T1
WHERE T1.range = T.range
AND (SELECT COUNT(*) FROM YourTable T2
WHERE T2.range = T1.range AND T2.colour < T1.colour) = 0)
AS quantity1,
(SELECT colour FROM YourTable T1
WHERE T1.range = T.range
AND (SELECT COUNT(*) FROM YourTable T2
WHERE T2.range = T1.range AND T2.colour < T1.colour) = 1)
AS colour2,
(SELECT size FROM YourTable T1
WHERE T1.range = T.range
AND (SELECT COUNT(*) FROM YourTable T2
WHERE T2.range = T1.range AND T2.colour < T1.colour) = 1)
AS size2,
(SELECT quantity FROM YourTable T1
WHERE T1.range = T.range
AND (SELECT COUNT(*) FROM YourTable T2
WHERE T2.range = T1.range AND T2.colour < T1.colour) = 1)
AS quantity2,
REPEAT FOR UP TO 20 colours!
FROM YourTable T
With a small change, this could order the colour/size/quantity sets
according to their original order, assuming you retain ColorNumber as
I proposed earlier. Each subquery would look like this instead of as
above:
(SELECT colour FROM YourTable T1
WHERE T1.range = T.range
AND (SELECT COUNT(*) FROM YourTable T2
WHERE T2.range = T1.range
AND T2.ColourNumber < T1.ColourNumber) = 0)
AS colour1,
Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts