Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rows and Columns

Status
Not open for further replies.

SPE2

Technical User
Dec 18, 2006
4
GB
I new member to this Forum and the question is more a ADP / SQL question but any help is much appreciated.

I have a table with multiple records that I need to reformat.

Colour Size Qty
Rec1 Red L 1
Rec2 Red M 2
Rec3 Red S 3
Rec4 Blue L 1
Rec5 Blue M 2
Rec6 Blue S 3

Transform to a new table or query

Colour L M S
Rec1 Red 1 2 3
Rec2 Blue 1 2 3

It is a partial cross tab but it is causing be grief!


 
Sorry,
Nothing other than embarrassing failures to show
I can’t seem to get away from the colour appearing multiple times

 
One way (SQL code):
SELECT Colour, Sum(QtyL) AS L, Sum(QtyM) AS M, Sum(QtyS) AS S
FROM (
SELECT Colour, Qty AS QtyL, 0 AS QtyM, 0 AS QtyS FROM yourTable WHERE size='L'
UNION SELECT Colour, 0, Qty, 0 FROM yourTable WHERE size='M'
UNION SELECT Colour, 0, 0, Qty FROM yourTable WHERE size='S'
) AS U
GROUP BY Colour

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 

Thanks PH,
Looks good but it is not happy with the first Sum line as it doesn’t like the colour field not being summed. I think it may also need some other punctuation. I really do appreciate your time in looking at this

SPE
 
What is your actual SQL code ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
My fault.. Missed out a table name, your code works fine both in mdb mode and Access Project mode with slight punctuation mods.

I never expected to get this sorted this evening. I can now scale it up to my 500,000 rec problem
Thanks again

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top