I have a query: (using a very simple example)
SELECT
ItemCategory,
ItemID,
Sales
FROM
MyTable
WHERE
CustomerID = 1
Which returns
ItemCategory | ItemID | Sales
A 0 75.00
A 1 50.00
A 2 0.00
B 3 25.00
B 4 25.00
C 5 20.00
C 6 30.00
C 7 10.00
C 8 0.00
C 9 50.00
How can I modify this query so that I receive the percentage of sales for each item grouped by ItemCategory?
That is, I would like this returned:
ItemCategory | ItemID | Sales | PercentageOfCategory
A 0 75.00 60%
A 1 50.00 40%
A 2 0.00 0%
B 3 25.00 50%
B 4 25.00 50%
C 5 20.00 20%
C 6 30.00 30%
C 7 10.00 10%
C 8 0.00 0%
C 9 50.00 50%
I tried to keep the example as trivial as possible, the actual query is pretty complex but I imagine the same logic still applies.
EDIT: I believe the server is sql server 2008
Assuming SQL Server 2005+ for the CTE: