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:
And produces this:
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.
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
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 > CONVERT(DATETIME, '2005-01-01 00:00:00', 102)) AND (Trans.TranTypeID = 'so' OR
Trans.TranTypeID = 'ca') AND (Trans.TranStatus <> 'v') AND (Item.ItemClassID = 'RHM')
GROUP BY Trans.TranSID, TransDetail.TranItemID
Which produces this:
But what I really need to do is transform this data so that the StoreID values become columns, like the Access CrossTab query above.