Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Pivot Expand / Collapse
Author
Message
Posted Monday, June 22, 2009 5:56 PM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Saturday, January 22, 2011 12:01 PM
Points: 702, 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
Post #739826
Posted Tuesday, June 23, 2009 1:13 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 7:22 AM
Points: 4,654, Visits: 11,115
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
Post #739940
Posted Tuesday, June 23, 2009 2:48 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, July 3, 2012 5:49 AM
Points: 418, Visits: 365
U need to use PIVOT function
Post #739990
Posted Tuesday, June 23, 2009 2:52 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 7:22 AM
Points: 4,654, Visits: 11,115
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
Post #739992
Posted Tuesday, June 23, 2009 4:22 AM


SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, July 3, 2012 5:49 AM
Points: 418, 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.
Post #740036
Posted Tuesday, June 23, 2009 4:31 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 7:22 AM
Points: 4,654, Visits: 11,115
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
Post #740042
Posted Tuesday, June 23, 2009 10:16 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Saturday, January 22, 2011 12:01 PM
Points: 702, 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
Post #740347
Posted Tuesday, June 23, 2009 10:25 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Yesterday @ 7:22 AM
Points: 4,654, Visits: 11,115
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
Post #740354
Posted Friday, December 11, 2009 2:24 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, December 9, 2014 3:40 PM
Points: 118, Visits: 569
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.
Post #833216
Posted Thursday, March 14, 2013 12:45 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, September 10, 2014 4:45 PM
Points: 308, Visits: 839
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.
Post #1431192
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse