﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / Development  / Pivot / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 19:20:04 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Pivot</title><link>http://www.sqlservercentral.com/Forums/Topic739826-145-1.aspx</link><description>Sorry if I am way off base, but from my understanding, the Pivot operator is for aggregating information at the cross section of a row and column.  Since dates are not aggregatable,unless you apply a count function (which in this case wouldn't make sense becuase you get only one invoice#1, #2, #3, etc), the pivot operator doesn't even make sense here.</description><pubDate>Thu, 14 Mar 2013 12:45:43 GMT</pubDate><dc:creator>polkadot</dc:creator></item><item><title>RE: Pivot</title><link>http://www.sqlservercentral.com/Forums/Topic739826-145-1.aspx</link><description>Gianluca, thanks for the tip. I've been trying to convert our Access queries to sp's and views. This will allow me to translate the crosstab queries in Access. I think I've got it down now.</description><pubDate>Fri, 11 Dec 2009 14:24:48 GMT</pubDate><dc:creator>SQLWannabe</dc:creator></item><item><title>RE: Pivot</title><link>http://www.sqlservercentral.com/Forums/Topic739826-145-1.aspx</link><description>I'm sorry, there's no way to get it with a PIVOT operation, because column names have to be explicitly defined.I'm just wondering why you need to get data that way: if it's just a presentation issue, you could reverse the logic of the loop that builds the output, running for columns vertically and for rows horizontally.The other option is to code the SELECT statement with a dynamic query, basically building the pivot string and then adding it to the base statement.</description><pubDate>Tue, 23 Jun 2009 10:25:12 GMT</pubDate><dc:creator>spaghettidba</dc:creator></item><item><title>RE: Pivot</title><link>http://www.sqlservercentral.com/Forums/Topic739826-145-1.aspx</link><description>Nice. Now how about if I have two columns InvoiceNumber, BillName, BillDate1, Amit , 1/1/19002,Amit, 1/2/1901and looking for output as1                 2Amit            Amit1/1/1900     1/2/1901Also, is there a way to get all invoices without using dynamic query?</description><pubDate>Tue, 23 Jun 2009 10:16:12 GMT</pubDate><dc:creator>Amit Lohia</dc:creator></item><item><title>RE: Pivot</title><link>http://www.sqlservercentral.com/Forums/Topic739826-145-1.aspx</link><description>[quote][b]arup_kc (6/23/2009)[/b][hr]Few days before i have used this pivot function and it worked efficiently.[/quote]It works, but it is quite useless if you want to pivot for a dynamic column list. I had to code for dynamic sql and sp_executesql to achieve it.Just a few words about performance: internally it does an aggregation on a CASE expression...[code][Expr1006] = Scalar Operator(MIN(CASE WHEN [InvoiceNumber]=(1) THEN [BillName] ELSE NULL END)); [Expr1007] = Scalar Operator(MIN(CASE WHEN [InvoiceNumber]=(2) THEN [BillName] ELSE NULL END)); [Expr1008] = Scalar Operator(MIN(CASE WHEN [InvoiceNumber]=(3) THEN [BillName] ELSE NULL END)); [Expr1009] = Scalar Operator(MIN(CASE WHEN [InvoiceNumber]=(4) THEN [BillName] ELSE NULL END))[/code](taken from the actual execution plan)Regards,Gianluca</description><pubDate>Tue, 23 Jun 2009 04:31:06 GMT</pubDate><dc:creator>spaghettidba</dc:creator></item><item><title>RE: Pivot</title><link>http://www.sqlservercentral.com/Forums/Topic739826-145-1.aspx</link><description>Hi sartorri,Sorry I have not seen ur post.I hope you are right and the code is also right. Few days before i have used this pivot function and it worked efficiently.</description><pubDate>Tue, 23 Jun 2009 04:22:17 GMT</pubDate><dc:creator>arup chakraborty</dc:creator></item><item><title>RE: Pivot</title><link>http://www.sqlservercentral.com/Forums/Topic739826-145-1.aspx</link><description>[quote][b]arup_kc (6/23/2009)[/b][hr]U need to use PIVOT function[/quote]...maybe as I suggested in my previous post?Or are you suggesting a different strategy?</description><pubDate>Tue, 23 Jun 2009 02:52:46 GMT</pubDate><dc:creator>spaghettidba</dc:creator></item><item><title>RE: Pivot</title><link>http://www.sqlservercentral.com/Forums/Topic739826-145-1.aspx</link><description>U need to use PIVOT function</description><pubDate>Tue, 23 Jun 2009 02:48:24 GMT</pubDate><dc:creator>arup chakraborty</dc:creator></item><item><title>RE: Pivot</title><link>http://www.sqlservercentral.com/Forums/Topic739826-145-1.aspx</link><description>You can use the PIVOT operator, but I think this is not as flexible as you would expect.Anyway this code does the trick. [code]DECLARE @Invoices TABLE (	InvoiceNumber int, 	BillName varchar(10))INSERT INTO @Invoices VALUES(1,'Amit')INSERT INTO @Invoices VALUES(2,'Amit')INSERT INTO @Invoices VALUES(3,'BBB')INSERT INTO @Invoices VALUES(4,'Amit')SELECT [1],[2],[3],[4]FROM (SELECT * FROM @Invoices) AS IPIVOT (	MIN(BillName) 	FOR InvoiceNumber IN ([1],[2],[3],[4])) AS pvt			[/code]You could make it more dynamic building a sql string in code adn running it with EXEC or sp_executesql.Hope this helps.Gianluca</description><pubDate>Tue, 23 Jun 2009 01:13:04 GMT</pubDate><dc:creator>spaghettidba</dc:creator></item><item><title>Pivot</title><link>http://www.sqlservercentral.com/Forums/Topic739826-145-1.aspx</link><description>Guys,I have a table as InvoiceNumber, BillName1, Amit 2, Amit3, BBB4, AmitThe BillName can be same or different.I need the output as follow1      2       3      4Amit  Amit  BBB   AmitAny idea how can we do this?</description><pubDate>Mon, 22 Jun 2009 17:56:03 GMT</pubDate><dc:creator>Amit Lohia</dc:creator></item></channel></rss>