title: SQL Server : SELECT Column A from Column B
body: <p>First, I hope the topic title wasn't too vague, I'm not really sure how to describe this issue.</p>
  
  <p>I have been handed an inventory database that was grown and developed by a few generations of people with varying levels of skill. Now it have tons of gotcha's of things that look alike but aren't quite.</p>
  
  <p>So, while there is a table with cost adjustments for every type of inventory. It is unused instead someone took the time to pre-calculate every possible price and add it to each line of the inventory table: </p>
  
  <pre><code>SELECT Part#, CategoryID, StdCost, ListPrice, Level1, Level2, Level3 
  FROM [Inventory] 
  WHERE ...
  </code></pre>
  
  <p>Each row of the <code>[Customers]</code> table has a <code>PriceLevel</code> column that contains one of these values: <code>Level1, Level2, Level3</code>. But now I just found out there is yet another table, for a small subset of customers that get additional discounts. Finding <code>E.DiscountLevel</code> with a <code>Left Join</code> was easy the help I am looking for is with the last column, this other discount table also overrides the customer <code>PriceLevel</code> with a new one for specific categories. I am not sure if it can be done at all or not, but can a cell value from one table be used as a column name in another table of the same <code>SELECT</code>?</p>
  
  <p>Table <code>[Inventory]</code> has columns <code>Level1, Level2, Level3</code>, table <code>[InventoryDiscounts]</code> has a column <code>PriceLevel</code> that contains the <em>Word</em> <code>Level1</code>. Should I just use a second query, or is it possible to somehow <code>SELECT B.(E.Column)</code></p>
  
  <p>This is the whole query with its many joins. No <strong>Bold</strong> inside comment blocks, but you can see the Stars around the trouble spot.</p>
  
  <pre><code>SELECT 
      A.InventoryID, B.Description, D.Category, 
      B.ListPrice, B.Level3 AS Price,
      E.DiscountLevel, **B.(E.PriceLevel) AS AltPrice**
  FROM 
      [BranchInventory] A 
  INNER JOIN 
      [Inventory] B ON A.InventoryID = B.InventoryID 
  INNER JOIN 
      [Branches] C ON A.BranchID = C.BranchID  
  INNER JOIN 
      [Categories] D ON B.CategoryID = D.CategoryID 
  LEFT JOIN 
      [InventoryDiscounts] E ON E.CustomerID = 32 AND B.CategoryID = E.CategoryID
  WHERE 
      A.BranchID = 8 
      AND A.InventoryID = 1181691
  </code></pre>
  
  <p>I expect the answer is going to be 'Just use an extra query', and that is ok. But frequently you guys are able to work some crazy magic, and I would love it if that was the case.</p>