Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL 2000 TSQL - Creating Pivot Table
    primarykey
    data
    text
    <p>I am really struggling with figuring out how to convert a MS Access CrossTab query into T-SQL to run on SQL2000 or into Linq-to-SQL. What I have is a query that looks like this in Access: </p> <p><img src="https://i.stack.imgur.com/b0Rhf.jpg" alt="Access Cross Tab Query"> And produces this: <img src="https://i.stack.imgur.com/hnUrt.jpg" alt="Access Cross Tab Query Results"></p> <p>The query basically groups by ItemID and StoreID and Sums the Qty sold, but with the CrossTab query in Access, I am able to have a unique ItemID for each row, a column for each StoreID, and the total Qty for each StoreID/ItemID combination as the value.</p> <p>How do you construct this in T-SQL 2000? I can build a simple select query with grouping, but it gives me the data in three columns, StoreID, ItemID, and Qty. But what I need is a column for ItemID and a column for every StoreID in the result set</p> <pre><code>SELECT Trans.TranSID as StoreID, TransDetail.TranItemID as ItemID, SUM(TransDetail.Qty) AS TotalQtyForStore FROM Trans INNER JOIN TransDetail ON Trans.TranID = TransDetail.TranID INNER JOIN Item ON TransDetail.TranItemID = Item.ItemID WHERE (Trans.TranDate &gt; CONVERT(DATETIME, '2005-01-01 00:00:00', 102)) AND (Trans.TranTypeID = 'so' OR Trans.TranTypeID = 'ca') AND (Trans.TranStatus &lt;&gt; 'v') AND (Item.ItemClassID = 'RHM') GROUP BY Trans.TranSID, TransDetail.TranItemID </code></pre> <p>Which produces this:</p> <p><img src="https://i.stack.imgur.com/q3SRM.jpg" alt="enter image description here"></p> <p>But what I really need to do is transform this data so that the StoreID values become columns, like the Access CrossTab query above.</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.
 

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