Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I suggest using <a href="http://blog.sqlauthority.com/2008/06/07/sql-server-pivot-and-unpivot-table-examples/" rel="nofollow noreferrer">PIVOT</a> function of T-SQL:</p> <pre><code>SELECT ItemId, ProductKey, Quantity, PaperType, Price FROM ( SELECT ItemId, ProductKey, [Key], Value FROM T ) AS yadayada PIVOT (MAX(Value) FOR [Key] IN (Quantity, PaperType, Price)) AS pvt </code></pre> <p>Result of PIVOT query</p> <p>(i believe this result is identical with <a href="https://stackoverflow.com/users/698082/revoua">@revoua</a>'s <a href="https://stackoverflow.com/a/20727428/1538014">query</a> with JOINs):</p> <pre><code> | ITEMID | PRODUCTKEY | QUANTITY | PAPERTYPE | PRICE | |--------|------------|----------|-----------|-------| | 1 | x | 50 | 1 | 25 | | 2 | x | 100 | 1 | 40 | | 3 | x | 250 | 1 | 80 | | 4 | x | 500 | 1 | 120 | | 5 | x | 1000 | 1 | 180 | | 6 | x | 3000 | 1 | 300 | | 7 | x | 50 | 2 | 30 | | 8 | x | 100 | 2 | 50 | | 9 | x | 250 | 2 | 100 | | 10 | x | 500 | 2 | 150 | | 11 | x | 1000 | 2 | 220 | | 12 | x | 3000 | 2 | 350 | | 13 | x | 50 | 3 | 35 | | 14 | x | 100 | 3 | 60 | | 15 | x | 250 | 3 | 120 | | 16 | x | 500 | 3 | 180 | | 17 | x | 1000 | 3 | 250 | | 18 | x | 3000 | 3 | 400 | </code></pre> <p>you can query this with a where clause like this:</p> <pre><code>WHERE PaperType = @PaperType AND Quantity = @Quantity </code></pre> <p>OR using in a cte</p> <pre><code>;WITH a1 AS ( SELECT ItemId, ProductKey, Quantity, PaperType, Price FROM ( SELECT ItemId, ProductKey, [Key], Value FROM T ) AS yadayada PIVOT (MAX(Value) FOR [Key] IN (Quantity, PaperType, Price)) AS pvt ) SELECT ItemId, Price, ProductKey FROM a1 WHERE PaperType = @PaperType AND Quantity = @Quantity </code></pre> <p><a href="http://www.sqlfiddle.com/#!6/0b703/45/0" rel="nofollow noreferrer">SQLFIDDLE</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload