消息102,级别15,状态1,第28行'order'附近的语法不正确

时间:2019-03-21 08:35:42

标签: sql sql-server tsql sql-server-2008 select

我正在尝试以下查询,但出现错误。我正在尝试在同一查询中计算YTD和以前的YTD。

  

消息102,级别15,状态1,第28行在“ order”附近的语法不正确。

WITH
    grouped_by_date AS
(
    SELECT
        [Sales_Organization],
        [Market_Grp],
        [Delivery_Year],
        [Delivery_Month],
        [Invoicing_Day],
        SUM(QTY_UoM)             AS Weight
    FROM
        tmp.factsales s
    GROUP BY
        [Sales_Organization],
        [Market_Grp],
        [Delivery_Year],
        [Delivery_Month],
        [Invoicing_Day]
),
    cumulative_sum_for_ytd AS
(
    SELECT
        *,


        SUM([Weight]) OVER (PARTITION BY [Delivery_Year] ORDER BY [Delivery_Month], [Invoicing_Day]
                           )
                              AS Weight_YTD
    FROM
        grouped_by_date
),


    hack_to_do_lag AS
(
    SELECT
        *,
        CASE
            WHEN [Delivery_Year]%2=1
            THEN MAX(CASE WHEN [Delivery_Year]%2=0 THEN [Weight_YTD] END) OVER (PARTITION BY ([Delivery_Year]+0)/2)
            ELSE MAX(CASE WHEN [Delivery_Year]%2=1 THEN [Weight_YTD] END) OVER (PARTITION BY ([Delivery_Year]+1)/2)
        END
            AS Weight_PreviousYTD
    FROM
        cumulative_sum_for_ytd
)
SELECT
    *
FROM
    hack_to_do_lag

我在Google上搜索过,实际上该问题链接了我使用的版本,

SELECT @@VERSION
  

Microsoft SQL Server 2008 R2(SP3)-10.50.6220.0(X64)2015年3月19日   12:32:14版权所有(c)Microsoft Corporation Enterprise Edition   Windows NT 6.3(内部版本9600:)(管理程序)上安装(64位)

我该如何解决我的问题?我无法更改版本。

1 个答案:

答案 0 :(得分:0)

SQL Server 2008不支持累积窗口函数,因此您需要进行不同的计算。子查询或var chosenName = choose_user.onItemSelected("name") 是一种典型方法:

apply