Answered by:
ORDER BY not allowed on Azure SQL

Question
-
Dear all,
I have the follwoing SQL server store procedure that I have tested and created under my local SQL server 2012
CREATE PROCEDURE [dbo].[RPT_GetRemainingStockByProduct] -- Add the parameters for the stored procedure here @StoreID int AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here SELECT *, SUM(Remaining) OVER (PARTITION BY StoreID ORDER BY ProductID) AS TotalRemainingAtStore FROM ( SELECT P.StoreID, P.InventoryDate, P.ProductID, SUM(PIT.Quantity * P.ItemUnitWeight) AS TotalWeight, SUM(PIT.UsedQuantity * P.ItemUnitWeight) AS UsedWeight, SUM(PIT.Quantity * P.ItemUnitWeight)-SUM(PIT.UsedQuantity * P.ItemUnitWeight) AS Remaining, Production.UnitMeasure.UnitMeasureCode, Sales.Store.Name, LOC.MaxQuantity as MaxStorage FROM Sales.Store INNER JOIN Production.ProductItem AS PIT INNER JOIN Production.Product AS P ON PIT.ProductID = P.ProductID INNER JOIN Production.UnitMeasure ON P.WeightUnitMeasureID = Production.UnitMeasure.UnitMeasureID ON Sales.Store.BusinessEntityID = P.StoreID INNER JOIN Production.Location AS LOC ON Sales.Store.BusinessEntityID = LOC.StoreID AND P.LocationID = LOC.LocationID AND P.LocationID = LOC.LocationID WHERE (P.IsDeleted = 0) AND (PIT.IsDeleted = 0) AND P.StoreID=@StoreID GROUP BY P.StoreID,P.InventoryDate, P.ProductID, Production.UnitMeasure.UnitMeasureCode,Sales.Store.Name,LOC.MaxQuantity )t END
When trying to create that store procs under my SQL azure db, I get an error message saying that ORDER BY is not allowed in line : SUM(Remaining) OVER (PARTITION BY StoreID ORDER BY ProductID) AS TotalRemainingAtStore
Any idea ?
regards
- Moved by Eric__Zhang Friday, November 27, 2015 2:02 AM Move to a proper forum for better result
Thursday, November 26, 2015 2:28 PM
Answers
-
- Edited by dsmwb Friday, November 27, 2015 5:44 PM
- Proposed as answer by Casey KarstMicrosoft employee Monday, November 30, 2015 6:08 PM
- Marked as answer by Casey KarstMicrosoft employee Thursday, December 3, 2015 4:29 PM
Friday, November 27, 2015 4:30 PM
All replies
-
Azure wont support ORDER BY along with PARTITION BY. You best bet is to implement logic using a correlated subquery
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
----------------------------
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook PageThursday, November 26, 2015 2:32 PM -
How should I change my querry then to get it work the same ? Sorry I am not an expert
regards
Friday, November 27, 2015 7:41 AM -
- Edited by dsmwb Friday, November 27, 2015 5:44 PM
- Proposed as answer by Casey KarstMicrosoft employee Monday, November 30, 2015 6:08 PM
- Marked as answer by Casey KarstMicrosoft employee Thursday, December 3, 2015 4:29 PM
Friday, November 27, 2015 4:30 PM -
To reiterate what dsmwb's link say, partition By Order by is supported on SQL Azure Database v12. Please make sure that your database is on a V12 server.
For more resources on the upgrade: https://azure.microsoft.com/en-us/documentation/articles/sql-database-upgrade-server-portal/
Monday, November 30, 2015 6:08 PM