Click here to Skip to main content
15,913,836 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Please help me about pivot i cant implement in sql sever Please give me a simple example
Posted
Updated 1-Oct-13 3:34am
v2

Hello,

you can just refer the article from Code Project

Simple Way To Use Pivot In SQL Query[^]

Thanks..
 
Share this answer
 
Comments
Maciej Los 1-Oct-13 9:03am    
+5!
Sergey Alexandrovich Kryukov 1-Oct-13 9:38am    
5ed.
—SA
Solution 1 by d4742 is very good.

More you'll find on MSDN[^] site. ;)

Other resources:
How to Perform Pivot Operation in T-SQL..?[^]
SQL SERVER – PIVOT and UNPIVOT Table Examples[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Oct-13 9:38am    
5ed.
—SA
Maciej Los 1-Oct-13 16:39pm    
Thank you, Sergey ;)
Please find the example below:

SQL
USE AdventureWorks2008
GO
-- Creating Test Table
CREATE TABLE Product(Cust VARCHAR(25), Product VARCHAR(20), QTY INT)
GO
-- 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)
GO

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
GO

DROP TABLE Product
GO
 
Share this answer
 

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