15,355,772 members
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
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?

## Solution 1

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

## Solution 2

Using Pivot[^]

Simple Way To Use Pivot In SQL Query[^]

Please post back queries if any.
Thanks

## Solution 3

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```