Link to home
Start Free TrialLog in
Avatar of dimensionav
dimensionavFlag for Mexico

asked on

How to reorganize an sql query?

HI

I have a query that gives me the following information:

Code, ProductName, Size
  1       Blouse              S
  2       Blouse              S
  3       Skirt                  M
  4       Pants                L

The idea is to reorganize the information like this:

ProductName, SizeS, SizeM, SizeL
 Blouse               2         0         0
 Skirt                   0         1         0
 Pants                 0         0         1

Any ideas?

Regards.
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

select productname,
sum (case when Size = 'S' then 1 else 0 end) SizeS
sum (case when Size = 'M' then 1 else 0 end) SizeM
sum (case when Size = 'L' then 1 else 0 end) SizeL
from <Table>
group by productName
you might want to use pivot query

try something like this:

SELECT * FROM (SELECT Title,Code,'Size'+Size as sz FROM Table_1) Data
PIVOT (COUNT(Code)
FOR sz IN([SizeS],[sizeM],[SizeL])) AS CountSize
ged325, you are missing some commas
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial