是否在SQL Azure中支持PARTITION BY ... ORDER BY

时间:2014-08-01 16:46:21

标签: sql sql-server azure

我真的很挣这个,很感激任何帮助!我有下面的SQL语句,它在SQL 2012中完美执行,但不在Azure中执行,并带有以下消息:

Msg 40517, Level 15, State 1, Line 2
Keyword or statement option 'ORDER BY' is not supported in this version of SQL Server.

从谷歌搜索我做过PARTITION BY ... ORDER BY是否支持? http://msdn.microsoft.com/en-us/library/ms187810.aspx

SQL Azure and support for CTE syntax?

我已经粘贴了以下失败的SQL代码:

SELECT        isnull(ROW_NUMBER() OVER (ORDER BY s.Id), 0) AS Id, DATEPART(YEAR, DATEADD(WEEK, s.SystemWeekId, GETDATE())) AS PredictedYear, DATEPART(MONTH, 
DATEADD(WEEK, s.SystemWeekId, GETDATE())) AS PredictedMonth, s.SystemWeekId AS SystemWeek, s.VehicleId, 
(SUM(s.AverageWeeklyKms) OVER (PARTITION BY s.VehicleId ORDER BY s.Id) + NextServiceKilometres) CalculatedKilometres, 
CAST(CASE WHEN (((FLOOR((SUM(s.AverageWeeklyKms) OVER (PARTITION BY s.VehicleId ORDER BY s.Id) + NextServiceKilometres) / 1000)) * 1000)) % PreventativeKilometreInterval = 0 THEN
    (SELECT        ServiceTypeId
      FROM            ServiceTypes
      WHERE        ServiceTypeDescription =
                                    (SELECT        ParameterValue
                                      FROM            SystemParameters
                                      WHERE        ParameterName = 'PreventativeService')) WHEN (((FLOOR((SUM(s.AverageWeeklyKms) OVER (PARTITION BY s.VehicleId
ORDER BY s.Id) + NextServiceKilometres) / 1000)) * 1000)) % MajorKilometreInterval = 0 THEN
    (SELECT        ServiceTypeId
      FROM            ServiceTypes
      WHERE        ServiceTypeDescription =
                                    (SELECT        ParameterValue
                                      FROM            SystemParameters
                                      WHERE        ParameterName = 'MajorService')) WHEN (((FLOOR((SUM(s.AverageWeeklyKms) OVER (PARTITION BY s.VehicleId
ORDER BY s.Id) + NextServiceKilometres) / 1000)) * 1000)) % MinorKilometreInterval = 0 THEN
    (SELECT        ServiceTypeId
      FROM            ServiceTypes
      WHERE        ServiceTypeDescription =
                                    (SELECT        ParameterValue
                                      FROM            SystemParameters
                                      WHERE        ParameterName = 'MinorService')) ELSE 0 END AS INT) AS ServiceTypeId
FROM            Temp  AS s

2 个答案:

答案 0 :(得分:1)

在Azure Sql数据库服务器版本V12中支持PARTITION BY ... ORDER BY: https://msdn.microsoft.com/en-us/library/ms187810.aspx

答案 1 :(得分:0)

不幸的是,SQL Azure不支持分区依据...按语法排序您将需要使用内部联接,这是一个非常好的资源: http://www.codeproject.com/Articles/300785/Calculating-simple-running-totals-in-SQL-Server