Is there anyway to make this run faster? Keeps timing out. Takes over 5 minutes when running through the new query window.
SELECT TOP (100) PERCENT item_no, loc, ISNULL(SUM(CASE WHEN month(doc_dt) = 1 AND year(doc_dt) = year(getdate()) AND source = 'O' AND
doc_type = 'I' THEN quantity END), 0) AS SoldPrd1, ISNULL(SUM(CASE WHEN month(doc_dt) = 2 AND year(doc_dt) = year(getdate()) AND
source = 'O' AND doc_type = 'I' THEN quantity END), 0) AS SoldPrd2, ISNULL(SUM(CASE WHEN month(doc_dt) = 3 AND year(doc_dt) = year(getdate())
AND source = 'O' AND doc_type = 'I' THEN quantity END), 0) AS SoldPrd3, ISNULL(SUM(CASE WHEN month(doc_dt) = 4 AND year(doc_dt)
= year(getdate()) AND source = 'O' AND doc_type = 'I' THEN quantity END), 0) AS SoldPrd4, ISNULL(SUM(CASE WHEN month(doc_dt) = 5 AND
year(doc_dt) = year(getdate()) AND source = 'O' AND doc_type = 'I' THEN quantity END), 0) AS SoldPrd5, ISNULL(SUM(CASE WHEN month(doc_dt)
= 6 AND year(doc_dt) = year(getdate()) AND source = 'O' AND doc_type = 'I' THEN quantity END), 0) AS SoldPrd6,
ISNULL(SUM(CASE WHEN month(doc_dt) = 7 AND year(doc_dt) = year(getdate()) AND source = 'O' AND doc_type = 'I' THEN quantity END), 0)
AS SoldPrd7, ISNULL(SUM(CASE WHEN month(doc_dt) = 8 AND year(doc_dt) = year(getdate()) AND source = 'O' AND doc_type = 'I' THEN quantity END),
0) AS SoldPrd8, ISNULL(SUM(CASE WHEN month(doc_dt) = 9 AND year(doc_dt) = year(getdate()) AND source = 'O' AND
doc_type = 'I' THEN quantity END), 0) AS SoldPrd9, ISNULL(SUM(CASE WHEN month(doc_dt) = 10 AND year(doc_dt) = year(getdate()) AND
source = 'O' AND doc_type = 'I' THEN quantity END), 0) AS SoldPrd10, ISNULL(SUM(CASE WHEN month(doc_dt) = 11 AND year(doc_dt) = year(getdate())
AND source = 'O' AND doc_type = 'I' THEN quantity END), 0) AS SoldPrd11, ISNULL(SUM(CASE WHEN month(doc_dt) = 12 AND year(doc_dt)
= year(getdate()) AND source = 'O' AND doc_type = 'I' THEN quantity END), 0) AS SoldPrd12
FROM dbo.iminvtrx_sql
GROUP BY item_no, loc
ORDER BY SoldPrd1 DESC