<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; PIVOT and UNPIVOT Table Examples</title>
	<atom:link href="http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:54:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Orbie</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-57461</link>
		<dc:creator>Orbie</dc:creator>
		<pubDate>Mon, 09 Nov 2009 16:57:48 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-57461</guid>
		<description>Hi Guys, 
I have the following 4 records which reflect the price of a product in 4 different stores. 

Rec # Commodity_Name  Product_Description Product_ID Price
1        AIRFRESHNER 200ML   MORNING MIST    31   2.99
2        AIRFRESHNER 200ML   MORNING MIST    42   1.99
3        AIRFRESHNER 200ML   MORNING MIST    83   3.99
4        AIRFRESHNER 200ML   MORNING MIST    94   4.99

Is it possible to display the above fields for only record 1 and only the price fields for records 2, 3 and 4 on the same line using the Pivot Statement? 

I&#039;m using SQL Server 2008 and just wondering if there much of an overhead using the Pivot Statement??

Thanks.</description>
		<content:encoded><![CDATA[<p>Hi Guys,<br />
I have the following 4 records which reflect the price of a product in 4 different stores. </p>
<p>Rec # Commodity_Name  Product_Description Product_ID Price<br />
1        AIRFRESHNER 200ML   MORNING MIST    31   2.99<br />
2        AIRFRESHNER 200ML   MORNING MIST    42   1.99<br />
3        AIRFRESHNER 200ML   MORNING MIST    83   3.99<br />
4        AIRFRESHNER 200ML   MORNING MIST    94   4.99</p>
<p>Is it possible to display the above fields for only record 1 and only the price fields for records 2, 3 and 4 on the same line using the Pivot Statement? </p>
<p>I&#8217;m using SQL Server 2008 and just wondering if there much of an overhead using the Pivot Statement??</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-56298</link>
		<dc:creator>Imran Mohammed</dc:creator>
		<pubDate>Thu, 01 Oct 2009 03:53:29 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-56298</guid>
		<description>@sansugoi

Since you did not mention, any input, I assumed input as STRONGLY AGREE,AGREE,NEUTRAL,DISAGREE. You can change it how ever you want.

Please follow the order of the input, comma seperated with no spaces. otherwise procedure might not execute.

So this is the procedure, check this out, and let us know if you have questions.  at the end of procedures, I have given, samples to execute, take a look of it.

-- Start of procedure.

Create Procedure USP_Generate_DynamicSQL (@OptionCompleteName  varchar(8000) )
AS
SET NOCOUNT ON 

-- First Lets seperate out comma seperated values and store in a table variable.
Declare @table table (Ident int Identity , OptionName varchar(40))
Declare @Length int
Declare @Store_Name varchar (40)

-- Start of Storing comma seperated values into a table variable

Set @length = 1 

While @length != 0
begin 
	Set @Store_Name   = NULL
	Set @length  = 1
	Set @Store_Name  = substring ( @OptionCompleteName , @length, case when (charindex (&#039;,&#039; , @OptionCompleteName)-1)= -1 then len(@OptionCompleteName) else (charindex (&#039;,&#039; , @OptionCompleteName)-1) End )

	insert into @table values (@Store_Name )

	set @length = (charindex (&#039;,&#039; , @OptionCompleteName)+1)

	If @length != 1 
		begin
			Set @OptionCompleteName  = substring ( @OptionCompleteName, @length, datalength (@OptionCompleteName) + 1 - @length)
			set @length = (charindex (&#039;,&#039; , @OptionCompleteName)+1)
		End

	Else 
		Begin
			Set @OptionCompleteName  = substring ( @OptionCompleteName , 1, datalength (@OptionCompleteName))
			Set @length = 0
		End 

End

-- End of Storing comma seperated values into a table variable


-- Lets build the first short select statement dynamically.
Declare @Select_Statement1 varchar(8000)
Set @Select_Statement1 = &#039;&#039;

select @Select_Statement1  = @Select_Statement1 +case when @Select_Statement1 = &#039;&#039; then &#039;&#039; else &#039;+&#039; End +&#039;P1.Option&#039;+convert (varchar, Ident)
from @table

-- print @Select_Statement1   


-- Now lets build our final select statement dynamically.

Declare @Select_Statement2 varchar(8000)

Set @Select_Statement2 = &#039;

SELECT P1.*, (&#039;+@Select_Statement1+&#039;) AS TotalVotes
FROM (
	SELECT
			Surveys.Question&#039;

Declare @count int 
Set @count = 1

Declare @Ident int
Declare @OptionName varchar(40)
While @Count =@FromDate
			and Convert(varchar(10),SurveyResults.SurveyDate,101) &lt;=@ToDate )
	GROUP BY Surveys.Question,Surveys.SurveyID ) AS P1&#039;

print @Select_Statement2

SET NOCOUNT OFF 

GO 
-- Sample to Execute 
Exec USP_Generate_DynamicSQL &#039;STRONGLY AGREE,AGREE,NEUTRAL,DISAGREE,STRONGLY&#039; 

Exec USP_Generate_DynamicSQL &#039;STRONGLY AGREE,AGREE,NEUTRAL,DISAGREE&#039; 


~ IM</description>
		<content:encoded><![CDATA[<p>@sansugoi</p>
<p>Since you did not mention, any input, I assumed input as STRONGLY AGREE,AGREE,NEUTRAL,DISAGREE. You can change it how ever you want.</p>
<p>Please follow the order of the input, comma seperated with no spaces. otherwise procedure might not execute.</p>
<p>So this is the procedure, check this out, and let us know if you have questions.  at the end of procedures, I have given, samples to execute, take a look of it.</p>
<p>&#8211; Start of procedure.</p>
<p>Create Procedure USP_Generate_DynamicSQL (@OptionCompleteName  varchar(8000) )<br />
AS<br />
SET NOCOUNT ON </p>
<p>&#8211; First Lets seperate out comma seperated values and store in a table variable.<br />
Declare @table table (Ident int Identity , OptionName varchar(40))<br />
Declare @Length int<br />
Declare @Store_Name varchar (40)</p>
<p>&#8211; Start of Storing comma seperated values into a table variable</p>
<p>Set @length = 1 </p>
<p>While @length != 0<br />
begin<br />
	Set @Store_Name   = NULL<br />
	Set @length  = 1<br />
	Set @Store_Name  = substring ( @OptionCompleteName , @length, case when (charindex (&#8216;,&#8217; , @OptionCompleteName)-1)= -1 then len(@OptionCompleteName) else (charindex (&#8216;,&#8217; , @OptionCompleteName)-1) End )</p>
<p>	insert into @table values (@Store_Name )</p>
<p>	set @length = (charindex (&#8216;,&#8217; , @OptionCompleteName)+1)</p>
<p>	If @length != 1<br />
		begin<br />
			Set @OptionCompleteName  = substring ( @OptionCompleteName, @length, datalength (@OptionCompleteName) + 1 &#8211; @length)<br />
			set @length = (charindex (&#8216;,&#8217; , @OptionCompleteName)+1)<br />
		End</p>
<p>	Else<br />
		Begin<br />
			Set @OptionCompleteName  = substring ( @OptionCompleteName , 1, datalength (@OptionCompleteName))<br />
			Set @length = 0<br />
		End </p>
<p>End</p>
<p>&#8211; End of Storing comma seperated values into a table variable</p>
<p>&#8211; Lets build the first short select statement dynamically.<br />
Declare @Select_Statement1 varchar(8000)<br />
Set @Select_Statement1 = &#8221;</p>
<p>select @Select_Statement1  = @Select_Statement1 +case when @Select_Statement1 = &#8221; then &#8221; else &#8216;+&#8217; End +&#8217;P1.Option&#8217;+convert (varchar, Ident)<br />
from @table</p>
<p>&#8211; print @Select_Statement1   </p>
<p>&#8211; Now lets build our final select statement dynamically.</p>
<p>Declare @Select_Statement2 varchar(8000)</p>
<p>Set @Select_Statement2 = &#8216;</p>
<p>SELECT P1.*, (&#8216;+@Select_Statement1+&#8217;) AS TotalVotes<br />
FROM (<br />
	SELECT<br />
			Surveys.Question&#8217;</p>
<p>Declare @count int<br />
Set @count = 1</p>
<p>Declare @Ident int<br />
Declare @OptionName varchar(40)<br />
While @Count =@FromDate<br />
			and Convert(varchar(10),SurveyResults.SurveyDate,101) &lt;=@ToDate )<br />
	GROUP BY Surveys.Question,Surveys.SurveyID ) AS P1&#039;</p>
<p>print @Select_Statement2</p>
<p>SET NOCOUNT OFF </p>
<p>GO<br />
&#8211; Sample to Execute<br />
Exec USP_Generate_DynamicSQL &#039;STRONGLY AGREE,AGREE,NEUTRAL,DISAGREE,STRONGLY&#039; </p>
<p>Exec USP_Generate_DynamicSQL &#039;STRONGLY AGREE,AGREE,NEUTRAL,DISAGREE&#039; </p>
<p>~ IM</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: sansugoi</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-56264</link>
		<dc:creator>sansugoi</dc:creator>
		<pubDate>Wed, 30 Sep 2009 06:50:43 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-56264</guid>
		<description>Hi,

I have a query regarding the cross tab query. I am generating a report in excel format e.g. survey report. curently I have create a stored procedure for that, everything is working fine for those 5 option for the survey question (Agree,disagree,neutral etc..) I have specified in the stored procedure,Now if admin enter one more option for the survey question answer, Now I don&#039;t want admin to go to the procedure code and from dotnet code to chnage everything.

Can We do this dynamically sso that when a admin add a new option for any wuestion then it should automaticalyyy display that option.

Can Anyone suggest me how to do that I here by pasting the code for my procedure..

SELECT P1.*, (P1.Option1 + P1.Option2 + P1.Option3 + P1.Option4 + P1.Option5) AS TotalVotes
FROM (SELECT     
Surveys.Question, 
--SurveyResults.SurveyOptionID,
    Option1 = COUNT(CASE WHEN SurveyOptions.OptionName=&#039;STRONGLY AGREE&#039; THEN SurveyResults.SurveyOptionID END), 
    Option2 = COUNT(CASE WHEN SurveyOptions.OptionName=&#039;AGREE&#039; THEN SurveyResults.SurveyOptionID END), 
    Option3 = COUNT(CASE WHEN SurveyOptions.OptionName=&#039;NEUTRAL&#039; THEN SurveyResults.SurveyOptionID END),
    Option4 = COUNT(CASE WHEN SurveyOptions.OptionName=&#039;DISAGREE&#039; THEN SurveyResults.SurveyOptionID END),
    Option5 = COUNT(CASE WHEN SurveyOptions.OptionName=&#039;STRONGLY DISAGREE&#039; THEN SurveyResults.SurveyOptionID END)
    
/*Surveys.OptionType,*/
--SurveyResults.UserID 
--SurveyOptions.OptionName 
--SurveyOptions.IsCorrect

FROM         
SurveyResults INNER JOIN
SurveyOptions ON SurveyResults.SurveyOptionID = SurveyOptions.SurveyOptionID INNER JOIN
Surveys ON SurveyOptions.SurveyID = Surveys.SurveyID


WHERE     
(Surveys.ModuleID = @ModuleID) 
and 
(SurveyResults.UserID  0)
and 
(Convert(varchar(10),SurveyResults.SurveyDate,101) &gt;=@FromDate 
and Convert(varchar(10),SurveyResults.SurveyDate,101) &lt;=@ToDate )


GROUP BY Surveys.Question,Surveys.SurveyID ) AS P1</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I have a query regarding the cross tab query. I am generating a report in excel format e.g. survey report. curently I have create a stored procedure for that, everything is working fine for those 5 option for the survey question (Agree,disagree,neutral etc..) I have specified in the stored procedure,Now if admin enter one more option for the survey question answer, Now I don&#8217;t want admin to go to the procedure code and from dotnet code to chnage everything.</p>
<p>Can We do this dynamically sso that when a admin add a new option for any wuestion then it should automaticalyyy display that option.</p>
<p>Can Anyone suggest me how to do that I here by pasting the code for my procedure..</p>
<p>SELECT P1.*, (P1.Option1 + P1.Option2 + P1.Option3 + P1.Option4 + P1.Option5) AS TotalVotes<br />
FROM (SELECT<br />
Surveys.Question,<br />
&#8211;SurveyResults.SurveyOptionID,<br />
    Option1 = COUNT(CASE WHEN SurveyOptions.OptionName=&#8217;STRONGLY AGREE&#8217; THEN SurveyResults.SurveyOptionID END),<br />
    Option2 = COUNT(CASE WHEN SurveyOptions.OptionName=&#8217;AGREE&#8217; THEN SurveyResults.SurveyOptionID END),<br />
    Option3 = COUNT(CASE WHEN SurveyOptions.OptionName=&#8217;NEUTRAL&#8217; THEN SurveyResults.SurveyOptionID END),<br />
    Option4 = COUNT(CASE WHEN SurveyOptions.OptionName=&#8217;DISAGREE&#8217; THEN SurveyResults.SurveyOptionID END),<br />
    Option5 = COUNT(CASE WHEN SurveyOptions.OptionName=&#8217;STRONGLY DISAGREE&#8217; THEN SurveyResults.SurveyOptionID END)</p>
<p>/*Surveys.OptionType,*/<br />
&#8211;SurveyResults.UserID<br />
&#8211;SurveyOptions.OptionName<br />
&#8211;SurveyOptions.IsCorrect</p>
<p>FROM<br />
SurveyResults INNER JOIN<br />
SurveyOptions ON SurveyResults.SurveyOptionID = SurveyOptions.SurveyOptionID INNER JOIN<br />
Surveys ON SurveyOptions.SurveyID = Surveys.SurveyID</p>
<p>WHERE<br />
(Surveys.ModuleID = @ModuleID)<br />
and<br />
(SurveyResults.UserID  0)<br />
and<br />
(Convert(varchar(10),SurveyResults.SurveyDate,101) &gt;=@FromDate<br />
and Convert(varchar(10),SurveyResults.SurveyDate,101) &lt;=@ToDate )</p>
<p>GROUP BY Surveys.Question,Surveys.SurveyID ) AS P1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phillip Senn</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-55105</link>
		<dc:creator>Phillip Senn</dc:creator>
		<pubDate>Fri, 21 Aug 2009 20:32:15 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-55105</guid>
		<description>You have a table called Product with a Field called Product.</description>
		<content:encoded><![CDATA[<p>You have a table called Product with a Field called Product.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: raghu</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-53612</link>
		<dc:creator>raghu</dc:creator>
		<pubDate>Fri, 10 Jul 2009 13:42:13 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-53612</guid>
		<description>can we write a select statement in IN OF PIVOT

PIVOT
( SUM(QTY)
FOR PRODUCT
IN (here i need to write select statement can we do it)
) AS pvt)

waiting 4 replay</description>
		<content:encoded><![CDATA[<p>can we write a select statement in IN OF PIVOT</p>
<p>PIVOT<br />
( SUM(QTY)<br />
FOR PRODUCT<br />
IN (here i need to write select statement can we do it)<br />
) AS pvt)</p>
<p>waiting 4 replay</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vickee</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-53034</link>
		<dc:creator>vickee</dc:creator>
		<pubDate>Tue, 16 Jun 2009 06:00:21 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-53034</guid>
		<description>Hi guys,


i have table like below ,

Product Name	Price	Date
Apple	1.5	5/5/2009
Apple	3	5/6/2009
Apple	3.5	5/7/2009
Apple	2.5	5/8/2009
Apple	5.5	5/9/2009
Orange	10.5	5/5/2009
Orange	12.5	5/6/2009
Orange	7.5	5/7/2009
Orange	4.5	5/8/2009
Orange	5.5	5/9/2009



I need output  like  below

Product Name	5/5/2009	5/6/2009	5/7/2009	5/8/2009	5/9/2009
					
Apple	1.5	3	3.5	2.5	5.5
Orange	10.5	12.5	7.5	4.5	5.5

also date increases column also need to increase,
Pls help me

Vickees</description>
		<content:encoded><![CDATA[<p>Hi guys,</p>
<p>i have table like below ,</p>
<p>Product Name	Price	Date<br />
Apple	1.5	5/5/2009<br />
Apple	3	5/6/2009<br />
Apple	3.5	5/7/2009<br />
Apple	2.5	5/8/2009<br />
Apple	5.5	5/9/2009<br />
Orange	10.5	5/5/2009<br />
Orange	12.5	5/6/2009<br />
Orange	7.5	5/7/2009<br />
Orange	4.5	5/8/2009<br />
Orange	5.5	5/9/2009</p>
<p>I need output  like  below</p>
<p>Product Name	5/5/2009	5/6/2009	5/7/2009	5/8/2009	5/9/2009</p>
<p>Apple	1.5	3	3.5	2.5	5.5<br />
Orange	10.5	12.5	7.5	4.5	5.5</p>
<p>also date increases column also need to increase,<br />
Pls help me</p>
<p>Vickees</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: vickee</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-53033</link>
		<dc:creator>vickee</dc:creator>
		<pubDate>Tue, 16 Jun 2009 06:00:06 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-53033</guid>
		<description>Hi,


i have table like below ,

Product Name	Price	Date
Apple	1.5	5/5/2009
Apple	3	5/6/2009
Apple	3.5	5/7/2009
Apple	2.5	5/8/2009
Apple	5.5	5/9/2009
Orange	10.5	5/5/2009
Orange	12.5	5/6/2009
Orange	7.5	5/7/2009
Orange	4.5	5/8/2009
Orange	5.5	5/9/2009



I need output  like  below

Product Name	5/5/2009	5/6/2009	5/7/2009	5/8/2009	5/9/2009
					
Apple	1.5	3	3.5	2.5	5.5
Orange	10.5	12.5	7.5	4.5	5.5

also date increases column also need to increase,
Pls help me

Vickees</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>i have table like below ,</p>
<p>Product Name	Price	Date<br />
Apple	1.5	5/5/2009<br />
Apple	3	5/6/2009<br />
Apple	3.5	5/7/2009<br />
Apple	2.5	5/8/2009<br />
Apple	5.5	5/9/2009<br />
Orange	10.5	5/5/2009<br />
Orange	12.5	5/6/2009<br />
Orange	7.5	5/7/2009<br />
Orange	4.5	5/8/2009<br />
Orange	5.5	5/9/2009</p>
<p>I need output  like  below</p>
<p>Product Name	5/5/2009	5/6/2009	5/7/2009	5/8/2009	5/9/2009</p>
<p>Apple	1.5	3	3.5	2.5	5.5<br />
Orange	10.5	12.5	7.5	4.5	5.5</p>
<p>also date increases column also need to increase,<br />
Pls help me</p>
<p>Vickees</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shashi</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-51081</link>
		<dc:creator>Shashi</dc:creator>
		<pubDate>Fri, 17 Apr 2009 17:15:40 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-51081</guid>
		<description>Hi 

I need to do the same PIVOT functionality in Sql 2000, can some one help me

Eg: 
Item  cp  amnt
A        R1  10
A        R2  10
B        R1   4
B        R2   5

I need this to be formatted like

Item  R1  R2
A       10  10
B       4     5

Thanks</description>
		<content:encoded><![CDATA[<p>Hi </p>
<p>I need to do the same PIVOT functionality in Sql 2000, can some one help me</p>
<p>Eg:<br />
Item  cp  amnt<br />
A        R1  10<br />
A        R2  10<br />
B        R1   4<br />
B        R2   5</p>
<p>I need this to be formatted like</p>
<p>Item  R1  R2<br />
A       10  10<br />
B       4     5</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lucky</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-47929</link>
		<dc:creator>Lucky</dc:creator>
		<pubDate>Tue, 03 Mar 2009 09:32:18 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-47929</guid>
		<description>article hold great content regarding pivot unpivot but the question always arises in my mind is that what is the exact defination of a Pivot and Unpivot table.....</description>
		<content:encoded><![CDATA[<p>article hold great content regarding pivot unpivot but the question always arises in my mind is that what is the exact defination of a Pivot and Unpivot table&#8230;..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shipu kumar</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-45729</link>
		<dc:creator>shipu kumar</dc:creator>
		<pubDate>Mon, 19 Jan 2009 11:11:25 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-45729</guid>
		<description>how to use pivot in sql server 2000,
if i am select varchar data type data in pivot</description>
		<content:encoded><![CDATA[<p>how to use pivot in sql server 2000,<br />
if i am select varchar data type data in pivot</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dev</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-45360</link>
		<dc:creator>Dev</dc:creator>
		<pubDate>Mon, 05 Jan 2009 11:28:05 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-45360</guid>
		<description>hi.. i have a table tblData containing columns  fanSerialNo,d1,d2,d3,d4..


this is how the table return values

fanSerialNo d1   d2     d3     d4
101              10   11    12     13

but i want the resultset like

fanserialNo    days    qty
101                 d1        10
101                 d2        11
101                 d3        12
101                 d4        13

Can anyone help me??
Thanx</description>
		<content:encoded><![CDATA[<p>hi.. i have a table tblData containing columns  fanSerialNo,d1,d2,d3,d4..</p>
<p>this is how the table return values</p>
<p>fanSerialNo d1   d2     d3     d4<br />
101              10   11    12     13</p>
<p>but i want the resultset like</p>
<p>fanserialNo    days    qty<br />
101                 d1        10<br />
101                 d2        11<br />
101                 d3        12<br />
101                 d4        13</p>
<p>Can anyone help me??<br />
Thanx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: atul</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-45274</link>
		<dc:creator>atul</dc:creator>
		<pubDate>Fri, 02 Jan 2009 07:14:10 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-45274</guid>
		<description>I have a table with two columns, labeled Year and Loss. In the Year
field, I have the numbers 1 to 10,000, each which can or cannot
repeat. In the Loss column, i have numbers corresponding to the
Years...for example:

Year, Loss
1, 568
1, 621
1, 358
1, 7888
2, 2689
2, 6563
2, 15
3, 983
3, 146
3, 258
3, 852
4, 96
5, 87
5, 32

So, you see, Year 1 can have four losses, Year 2 can have three
losses, etc.

Now, here&#039;s where I need help as I&#039;m not sure what to do given that I
am a beginner at SQl and learning: I want to have just one row for
each Year. So, for Year 1, I would like to have four extra columns,
and for Year 2, I would like to have three columns created to hold
loss numbers so they display in a row - please see below for example:

Year, Loss1, Loss2, Loss3, Loss4, Loss5, etc.
1, 568, 621, 358, 7888
2, 2689, 6563, 15
3, 983, 146, 258, 852
4, 96
5, 87, 32

I hope that the examples helped in clarifying what kind of Query I
need.
plz send me solution</description>
		<content:encoded><![CDATA[<p>I have a table with two columns, labeled Year and Loss. In the Year<br />
field, I have the numbers 1 to 10,000, each which can or cannot<br />
repeat. In the Loss column, i have numbers corresponding to the<br />
Years&#8230;for example:</p>
<p>Year, Loss<br />
1, 568<br />
1, 621<br />
1, 358<br />
1, 7888<br />
2, 2689<br />
2, 6563<br />
2, 15<br />
3, 983<br />
3, 146<br />
3, 258<br />
3, 852<br />
4, 96<br />
5, 87<br />
5, 32</p>
<p>So, you see, Year 1 can have four losses, Year 2 can have three<br />
losses, etc.</p>
<p>Now, here&#8217;s where I need help as I&#8217;m not sure what to do given that I<br />
am a beginner at SQl and learning: I want to have just one row for<br />
each Year. So, for Year 1, I would like to have four extra columns,<br />
and for Year 2, I would like to have three columns created to hold<br />
loss numbers so they display in a row &#8211; please see below for example:</p>
<p>Year, Loss1, Loss2, Loss3, Loss4, Loss5, etc.<br />
1, 568, 621, 358, 7888<br />
2, 2689, 6563, 15<br />
3, 983, 146, 258, 852<br />
4, 96<br />
5, 87, 32</p>
<p>I hope that the examples helped in clarifying what kind of Query I<br />
need.<br />
plz send me solution</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: indraes</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-44774</link>
		<dc:creator>indraes</dc:creator>
		<pubDate>Sat, 13 Dec 2008 05:40:39 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-44774</guid>
		<description>hi..
code sql nya kerennnnnnnnnnnnnnn banget dah
sangat bermamfaat..

salam dari Indonesia....</description>
		<content:encoded><![CDATA[<p>hi..<br />
code sql nya kerennnnnnnnnnnnnnn banget dah<br />
sangat bermamfaat..</p>
<p>salam dari Indonesia&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eve</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-44150</link>
		<dc:creator>Eve</dc:creator>
		<pubDate>Tue, 11 Nov 2008 09:16:31 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-44150</guid>
		<description>Hi ,
Very useful concept. It helped me a lot !!!!</description>
		<content:encoded><![CDATA[<p>Hi ,<br />
Very useful concept. It helped me a lot !!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: latha</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-43863</link>
		<dc:creator>latha</dc:creator>
		<pubDate>Wed, 22 Oct 2008 15:57:02 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-43863</guid>
		<description>hi

  iam new to this website.i want to learn sqlserver.2005 or 2008 which one is better.can any one give an idea how to start and where to start.plz help me
thanks in advance</description>
		<content:encoded><![CDATA[<p>hi</p>
<p>  iam new to this website.i want to learn sqlserver.2005 or 2008 which one is better.can any one give an idea how to start and where to start.plz help me<br />
thanks in advance</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shree</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-43578</link>
		<dc:creator>Shree</dc:creator>
		<pubDate>Tue, 07 Oct 2008 19:24:25 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-43578</guid>
		<description>Hi,
     I have two Columns State and City. When I select them from table, it displays like
	 STATE	 CITY
 	Connecticut	Hartford
	Connecticut 	Stamford
	Connecticut	New Haven
	Connecticut	Bridgeport
But I need them as
	 STATE	 CITY1	 CITY2	 CITY3	 CITY4
	Connecticut	Hartford  	Stamford 	New Haven	 Bridgeport
So that I can use them in my SP. There are many cities for some states.
Can you please help me out by using Pivot?</description>
		<content:encoded><![CDATA[<p>Hi,<br />
     I have two Columns State and City. When I select them from table, it displays like<br />
	 STATE	 CITY<br />
 	Connecticut	Hartford<br />
	Connecticut 	Stamford<br />
	Connecticut	New Haven<br />
	Connecticut	Bridgeport<br />
But I need them as<br />
	 STATE	 CITY1	 CITY2	 CITY3	 CITY4<br />
	Connecticut	Hartford  	Stamford 	New Haven	 Bridgeport<br />
So that I can use them in my SP. There are many cities for some states.<br />
Can you please help me out by using Pivot?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Vinoth</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-43423</link>
		<dc:creator>Vinoth</dc:creator>
		<pubDate>Wed, 01 Oct 2008 09:05:57 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-43423</guid>
		<description>Hi Dave,

It is really interesting  concept. I would like to learn more about this concept. Where can I get more information about PIVOT and UNPIVOT?</description>
		<content:encoded><![CDATA[<p>Hi Dave,</p>
<p>It is really interesting  concept. I would like to learn more about this concept. Where can I get more information about PIVOT and UNPIVOT?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER - Example of PIVOT UNPIVOT Cross Tab Query in Different SQL Server Versions Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-43416</link>
		<dc:creator>SQL SERVER - Example of PIVOT UNPIVOT Cross Tab Query in Different SQL Server Versions Journey to SQL Authority with Pinal Dave</dc:creator>
		<pubDate>Wed, 01 Oct 2008 01:30:25 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-43416</guid>
		<description>[...] SQL SERVER - PIVOT and UNPIVOT Table Examples SQL SERVER - PIVOT Table Example SQL SERVER - UNPIVOT Table Example [...]</description>
		<content:encoded><![CDATA[<p>[...] SQL SERVER &#8211; PIVOT and UNPIVOT Table Examples SQL SERVER &#8211; PIVOT Table Example SQL SERVER &#8211; UNPIVOT Table Example [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RAVINDRA</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-43160</link>
		<dc:creator>RAVINDRA</dc:creator>
		<pubDate>Tue, 23 Sep 2008 14:01:18 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-43160</guid>
		<description>Hi Dave,
upto now what i have seen the blog&#039;s,this is the goodone.
keep it up.
it&#039;s a very useful one.
Cheers,
Ravindra</description>
		<content:encoded><![CDATA[<p>Hi Dave,<br />
upto now what i have seen the blog&#8217;s,this is the goodone.<br />
keep it up.<br />
it&#8217;s a very useful one.<br />
Cheers,<br />
Ravindra</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marty</title>
		<link>http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/#comment-42694</link>
		<dc:creator>Marty</dc:creator>
		<pubDate>Thu, 11 Sep 2008 17:46:25 +0000</pubDate>
		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=638#comment-42694</guid>
		<description>I am having a problem with the Pivot...is there something wrong with my syntax...it just returns the column names as values...

Select BidId,&#039;Cancelled&#039;,
&#039;In Progress&#039;,
&#039;Loss&#039;,
&#039;New&#039;,
&#039;No Bid&#039;,
&#039;Referred Out&#039;
FROM  (
Select bi.BidId, dbo.Status.Name, dbo.StatusComments.CommentDate 
		From Bids bi INNER JOIN
               dbo.BidFormOwning ON bi.BidId = dbo.BidFormOwning.BidId INNER JOIN
               dbo.BidRevision ON dbo.BidFormOwning.BidFormOwningId = dbo.BidRevision.BidFormOwningId INNER JOIN
               dbo.StatusComments ON dbo.BidRevision.BidRevisionId = dbo.StatusComments.RevisionId INNER JOIN
               dbo.SupportTeams ON dbo.BidFormOwning.SupportTeamId = dbo.SupportTeams.SupportTeamId INNER JOIN
               dbo.Status ON dbo.StatusComments.StatusId = dbo.Status.StatusId
where dbo.BidFormOwning.SupportTeamId=2
and dbo.Status.Name in (&#039;Cancelled&#039;,
&#039;In Progress&#039;,
&#039;Loss&#039;,
&#039;New&#039;,
&#039;No Bid&#039;,
&#039;Referred Out&#039;)) as a
Pivot
(
Count(CommentDate)
FOR Name In ([Cancelled],
[In Progress],
[Loss],
[New],
[No Bid],
[Referred Out])) as b
order by BidId</description>
		<content:encoded><![CDATA[<p>I am having a problem with the Pivot&#8230;is there something wrong with my syntax&#8230;it just returns the column names as values&#8230;</p>
<p>Select BidId,&#8217;Cancelled&#8217;,<br />
&#8216;In Progress&#8217;,<br />
&#8216;Loss&#8217;,<br />
&#8216;New&#8217;,<br />
&#8216;No Bid&#8217;,<br />
&#8216;Referred Out&#8217;<br />
FROM  (<br />
Select bi.BidId, dbo.Status.Name, dbo.StatusComments.CommentDate<br />
		From Bids bi INNER JOIN<br />
               dbo.BidFormOwning ON bi.BidId = dbo.BidFormOwning.BidId INNER JOIN<br />
               dbo.BidRevision ON dbo.BidFormOwning.BidFormOwningId = dbo.BidRevision.BidFormOwningId INNER JOIN<br />
               dbo.StatusComments ON dbo.BidRevision.BidRevisionId = dbo.StatusComments.RevisionId INNER JOIN<br />
               dbo.SupportTeams ON dbo.BidFormOwning.SupportTeamId = dbo.SupportTeams.SupportTeamId INNER JOIN<br />
               dbo.Status ON dbo.StatusComments.StatusId = dbo.Status.StatusId<br />
where dbo.BidFormOwning.SupportTeamId=2<br />
and dbo.Status.Name in (&#8216;Cancelled&#8217;,<br />
&#8216;In Progress&#8217;,<br />
&#8216;Loss&#8217;,<br />
&#8216;New&#8217;,<br />
&#8216;No Bid&#8217;,<br />
&#8216;Referred Out&#8217;)) as a<br />
Pivot<br />
(<br />
Count(CommentDate)<br />
FOR Name In ([Cancelled],<br />
[In Progress],<br />
[Loss],<br />
[New],<br />
[No Bid],<br />
[Referred Out])) as b<br />
order by BidId</p>
]]></content:encoded>
	</item>
</channel>
</rss>
