Click here to Skip to main content
15,355,772 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Sir i need convert row in columns ,sir i have 1 row more than 40 column i need show it in as columns. sir i read some article related pivot but i couldn't understand. thanks
Posted
Comments
KaushalJB 1-Jan-15 2:30am
   
yes sir.. so simple sir : Simple Way To Use Pivot In SQL Query
Maciej Los 1-Jan-15 17:42pm
   
What is the question?

You ahve to use Pivot to convert rows into columns - Pivot Example in SQL Server[^].
   
Using Pivot[^]

Simple Way To Use Pivot In SQL Query[^]

The above two links would help you.
Please post back queries if any.
Thanks
   
Please find below example.
Hope it would help you to understand PIVOT

SQL
CREATE TABLE #Product(Cust VARCHAR(25), Product VARCHAR(20), QTY INT)

-- Inserting Data into Table
INSERT INTO #Product(Cust, Product, QTY)
VALUES('KATE','VEG',2)
INSERT INTO #Product(Cust, Product, QTY)
VALUES('KATE','SODA',6)
INSERT INTO #Product(Cust, Product, QTY)
VALUES('KATE','MILK',1)
INSERT INTO #Product(Cust, Product, QTY)
VALUES('KATE','BEER',12)
INSERT INTO #Product(Cust, Product, QTY)
VALUES('FRED','MILK',3)
INSERT INTO #Product(Cust, Product, QTY)
VALUES('FRED','BEER',24)
INSERT INTO #Product(Cust, Product, QTY)
VALUES('KATE','VEG',3)

SELECT *
FROM #Product

--First Example : 
SELECT PRODUCT, FRED, KATE
FROM (
SELECT CUST, PRODUCT, QTY
FROM #Product) up
PIVOT (SUM(QTY) FOR CUST IN (FRED, KATE)) AS pvt
ORDER BY PRODUCT

--Second Example : 
SELECT CUST, VEG, SODA, MILK, BEER, CHIPS
FROM (
SELECT CUST, PRODUCT, QTY
FROM #Product) up
PIVOT (SUM(QTY) FOR PRODUCT IN (VEG, SODA, MILK, BEER, CHIPS)) AS pvt
ORDER BY CUST
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900