SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Pivot


Pivot

Author
Message
Amit Lohia
Amit Lohia
SSC Eights!
SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)

Group: General Forum Members
Points: 988 Visits: 174
Guys,

I have a table as

InvoiceNumber, BillName
1, Amit
2, Amit
3, BBB
4, Amit

The BillName can be same or different.

I need the output as follow

1 2 3 4
Amit Amit BBB Amit

Any idea how can we do this?


Kindest Regards,

Amit Lohia
Gianluca Sartori
Gianluca Sartori
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10342 Visits: 13351
You can use the PIVOT operator, but I think this is not as flexible as you would expect.
Anyway this code does the trick.


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 I
PIVOT (
MIN(BillName)
FOR InvoiceNumber IN ([1],[2],[3],[4])
) AS pvt




You could make it more dynamic building a sql string in code adn running it with EXEC or sp_executesql.

Hope this helps.
Gianluca

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
arup chakraborty
arup chakraborty
Mr or Mrs. 500
Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)

Group: General Forum Members
Points: 590 Visits: 365
U need to use PIVOT function
Gianluca Sartori
Gianluca Sartori
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10342 Visits: 13351
arup_kc (6/23/2009)
U need to use PIVOT function


...maybe as I suggested in my previous post?
Or are you suggesting a different strategy?

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
arup chakraborty
arup chakraborty
Mr or Mrs. 500
Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)Mr or Mrs. 500 (590 reputation)

Group: General Forum Members
Points: 590 Visits: 365
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.
Gianluca Sartori
Gianluca Sartori
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10342 Visits: 13351
arup_kc (6/23/2009)
Few days before i have used this pivot function and it worked efficiently.


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


[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))


(taken from the actual execution plan)

Regards,
Gianluca

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Amit Lohia
Amit Lohia
SSC Eights!
SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)SSC Eights! (988 reputation)

Group: General Forum Members
Points: 988 Visits: 174
Nice. Now how about if I have two columns

InvoiceNumber, BillName, BillDate
1, Amit , 1/1/1900
2,Amit, 1/2/1901


and looking for output as

1 2
Amit Amit
1/1/1900 1/2/1901


Also, is there a way to get all invoices without using dynamic query?


Kindest Regards,

Amit Lohia
Gianluca Sartori
Gianluca Sartori
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10342 Visits: 13351
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.

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
SQLWannabe
SQLWannabe
SSC Veteran
SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)SSC Veteran (242 reputation)

Group: General Forum Members
Points: 242 Visits: 571
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.
polkadot
polkadot
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1140 Visits: 1114
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search