Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Manipulating Data into New Columns

Status
Not open for further replies.

warpped

MIS
Jan 19, 2000
59
US
I have a table that basically look like this:

Item# Test# Val1 Val2 Val3 Val4 Val5

12345 QA-01 12.1 12.2 13.2 NULL NULL
12345 QA-02 EX EX EX GD GD
12345 QA-03 45 32 52
45678 QA-02 EX GD EX GD EX
78945 QA-01 15 14 10

I need to put this is a report with columns by test#, with the values under the test number. I can do this, but the test numbers do not line up nicely. Is there a way to write the query so that I can make new columns with this data like :

Item# Test# QA01Val1 QA01Val2 QA01Val3 QA02Val1 QA02Val2 QA02Val3 QA02Val4 QA02Val5 QA03Val1 ...etc


Help, please..
 
Code:
SELECT
      T.ItemNum,

      QA01Val1 = Q1.Val1,
      QA01Val2 = Q1.Val2,
      QA01Val3 = Q1.Val3,

      QA02Val1 = Q2.Val1,
      QA02Val2 = Q2.Val2,
      QA02Val3 = Q2.Val3,
      QA02Val4 = Q2.Val4,
      QA02Val5 = Q2.Val5,

      QA03Val1 = Q3.Val1,
      QA03Val2 = Q3.Val2,

      [green]{...}[/green]
   FROM (SELECT DISTINCT ItemNum FROM TestData) T
      LEFT JOIN TestData Q1 ON T.ItemNum = Q1.ItemNum AND Q1.TestNum = 'QA-01'
      LEFT JOIN TestData Q2 ON T.ItemNum = Q2.ItemNum AND Q2.TestNum = 'QA-02'
      LEFT JOIN TestData Q3 ON T.ItemNum = Q3.ItemNum AND Q3.TestNum = 'QA-03'
      [green]{...}[/green]
   WHERE AnySpecialConditions

But dang that's a mess... and make sure you don't have more than one TestNum + ItemNum combination or you'll get two rows for one ItemNum.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top