June 22, 2009 at 5:56 pm
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?
Amit Lohia
June 23, 2009 at 1:13 am
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
June 23, 2009 at 2:48 am
U need to use PIVOT function
June 23, 2009 at 2:52 am
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
June 23, 2009 at 4:22 am
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.
June 23, 2009 at 4:31 am
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
June 23, 2009 at 10:16 am
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?
Amit Lohia
June 23, 2009 at 10:25 am
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
December 11, 2009 at 2:24 pm
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.
March 14, 2013 at 12:45 pm
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.
--Quote me
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply