Solved

Pivot table in sql 2008

Posted on 2010-08-17
3
774 Views
Last Modified: 2012-05-10
Hi,

I am not able to use pivot query to produce desired result. Please help me to get reqult that I want using Pivot query.  I have added the table script and data  in the code Snippet. I want to display the information like table given as follow using Pivot query.  
Qty_Rang      red        green    blue        black      
0-10                      12         13              14        15

10-20              16          17        18        -

20-30              20          21         22       -
Column names ( red, green blue....) are not fix it can increase or decrease. I need a dynamic Pivot query for this.

thanks,
waiting for your reply.
CREATE TABLE [dbo].[testPivo](

	[pid] [int] IDENTITY(1,1) NOT NULL,

	[qty_from] [int] NOT NULL,

	[qty_to] [int] NOT NULL,

	[color] [varchar](50) NOT NULL,

	[price] [decimal](9, 2) NULL,

 CONSTRAINT [PK_testPivo_1] PRIMARY KEY CLUSTERED 

(

	[pid] ASC

)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

) ON [PRIMARY]







insert into testPivo values(0,10,'red',12)

insert into testPivo values(0,10,'green',13)

insert into testPivo values(0,10,'blue',14)

insert into testPivo values(10,20,'red',16)

insert into testPivo values(10,20,'green',17)

insert into testPivo values(10,20,'blue',18)

insert into testPivo values(20,30,'red',20)

insert into testPivo values(20,30,'green',21)

insert into testPivo values(20,30,'blue',22)

insert into testPivo values(0,10,'black',15)

Open in new window

0
Comment
Question by:Gurbirs
3 Comments
 
LVL 7

Accepted Solution

by:
rashmi_vaghela earned 500 total points
ID: 33452757
hello
try this.

DECLARE @Columns varchar(8000)



DECLARE @SQL varchar(8000)

SET @Columns = substring((select ',['+color+']' from testPivo group by color for xml path('')),2,8000)



SET @SQL = 'SELECT * FROM 

  (Select qty_From ,COLOR,qty_to,price from testPivo) sourcedata

PIVOT

  (max(price) for color in ('+@Columns+')) pivottable'

EXEC(@sql)

GO

Open in new window

0
 

Author Comment

by:Gurbirs
ID: 33453072
Thanks alot . I have made a very little change according to my requirement.
 
Thanks alot once again.

DECLARE @Columns varchar(8000)



DECLARE @SQL varchar(8000)

SET @Columns = substring((select ',['+color+']' from testPivo group by color for xml path('')),2,8000)

SET @SQL = 'SELECT * FROM 

  (Select (CAST(qty_from as varchar)+''-''+ cast(qty_to as varchar))as Qty_Rang ,COLOR,qty_to,price from testPivo) sourcedata

PIVOT

  (max(price) for color in ('+@Columns+')) pivottable'

  print @sql

  

EXEC(@sql)

GO

Open in new window

0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
encyps queries mssql 15 37
Row-Level Security 2 32
Syntax using Declare 4 38
Generate Weekly Schedule 15 13
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

15 Experts available now in Live!

Get 1:1 Help Now