Link to home
Start Free TrialLog in
Avatar of trifecta2k
trifecta2k

asked on

Add sequential columns to an existing query based on rows of another table

I am trying to add columns on the fly to a query.  I'm using MS SQL.  To keep it simple here is some sample data

Table 1
Item_number | Description
11111              | Test1
22222              | Test2

Table 2
Item_number  | Class   |   Weight
11111                |    C      |    2
11111                |    B      |    3
22222                |    A      |   4

I would like to write a query that would produce the following results..
Item_number  | Class1  | Weight1  | Class2  | Weight2
11111               |  C           |     2           |    B        |   3
22222               |   A           |    4           |  NULL   |  NULL

Some cases there could be 10-15 different classes and weights and some might only be 1.  So it would have to be dynamic.

Is this possible?  If so, how?
ASKER CERTIFIED SOLUTION
Avatar of YZlat
YZlat
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trifecta2k
trifecta2k

ASKER

That worked perfectly.  Thanks.
this line:
order by Class
may need to be:
order by Class Desc