Solved

How to reorganize an sql query?

Posted on 2013-12-09
4
118 Views
Last Modified: 2013-12-14
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.
0
Comment
Question by:dimensionav
  • 3
4 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 39706184
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
0
 
LVL 35

Expert Comment

by:YZlat
ID: 39706199
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
0
 
LVL 35

Expert Comment

by:YZlat
ID: 39706232
ged325, you are missing some commas
0
 
LVL 35

Accepted Solution

by:
YZlat earned 500 total points
ID: 39706237
oops, Title should be ProductName:

SELECT * FROM (SELECT ProductName,Code,'Size'+Size as sz FROM Table_1) Data
PIVOT (COUNT(Code)
FOR sz IN([SizeS],[sizeM],[SizeL])) AS CountSize 

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now