Pivot

  • 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

  • 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

  • U need to use PIVOT function

  • 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

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

  • 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

  • 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

  • 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

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

  • 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