Query to display dif values from the same column in the same table -t-sql

  • I have a table with a column AttributeNumber and a column AttributeValue. The data is like this:

    OrderNo. AttributeNumber AttributeValue

    1.-Order_1 2001 A

    2.-Order_1 2002 B

    3.-Order_1 2003 C

    4.-Order_2 2001 A

    5.-Order_2 2002 B

    6.-Order_2 2003 C

    So the logic is as follows:

    I need to display in my query the values are coming from Order_1, means AttributreValues coming from AttibuteNumbers: 2001,2002,2003...and Order_2 the same thing.

    Not sure how to create my Select here since the values are in the same table 🙁

  • montserrat.deza (9/18/2015)


    I need to display in my query the values are coming from Order_1, means AttributreValues coming from AttibuteNumbers: 2001,2002,2003...and Order_2 the same thing.

    Is this what you are looking for?

    declare @t table

    (

    OrderNum char(10),

    AttrNumber int,

    AttrValue char

    )

    Insert @t (OrderNum, AttrNumber, AttrValue) VALUES

    ('Order_1', 2001, 'A'),

    ('Order_1', 2002, 'B'),

    ('Order_1', 2003, 'C'),

    ('Order_2', 2001, 'A'),

    ('Order_2', 2002, 'B'),

    ('Order_2', 2003, 'C')

    select *

    from

    (

    Select OrderNum, AttrNumber, AttrValue

    from @t

    ) src

    pivot

    (

    sum(AttrNumber)

    for AttrValue in ([A], , [C])

    ) piv;

    __________________________________________________________________________________________________________
    How to Post to get the most: http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thank you so much!!! I was looking for the pivot structure!! this works..thank you!

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply