Hi!I have a query that is performing poorly and that I would like to rewrite in a more efficient way. What I want is to calculate some statistics. Below is some example code illustrating the problem:CREATE TABLE #Temp( ID INT, ProductID int, Quantity int)INSERT INTO #TempVALUES( 1, 1, 23),( 2, 1, 33),( 3, 1, 6),( 4, 1, 7),( 5, 2, 15),( 6, 2, 6),( 7, 2, 13),( 8, 3, 21),( 9, 3, 16),( 10, 4, 3),( 11, 4, 11),( 12, 4, 9),( 13, 4, 14)SELECT ProductID, COUNT(*)FROM #TempWHERE Quantity < 10GROUP BY ProductIDSELECT ProductID, COUNT(*)FROM #TempWHERE Quantity < 15GROUP BY ProductIDSELECT ProductID, COUNT(*)FROM #TempWHERE Quantity < 20GROUP BY ProductIDSELECT ProductID, COUNT(*)FROM #TempWHERE Quantity < 25GROUP BY ProductIDDROP TABLE #Temp
It must be possible to rewrite this in a more efficient way?Appreciating any input on this
/HappyMelon