在SqlServer中

时间:2015-09-29 15:06:30

标签: sql-server

我有一个查询来查找数字语句的演变,我在我的语句表中有。我使用以下查询来循环它

SELECT LAST_UPDATED_WEEK, SUM( NUM_STATEMENTS ) OVER (ORDER BY LAST_UPDATED_WEEK ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS "Number of account statements"
    FROM (
      SELECT DATEPART(wk, STATEMENT_CREATION_DATE) AS LAST_UPDATED_WEEK,
             COUNT(DISTINCT STATEMENT_ID) AS NUM_STATEMENTS
      FROM   STATEMENTS_TABLE
      GROUP BY DATEPART(wk, STATEMENT_CREATION_DATE)

此查询的目的是接收每周总数的语句,如下面的: Table Data

例如,在8月31日这一周,我有3个新陈述。在9月7日这一周,我有4个新的陈述,这使得陈述的数量为7.这对于我表中的每一条记录都是

但是当我运行查询时,我得到以下错误:

  

错误:')'附近的语法不正确。   SQLState:S0001   ErrorCode:102

有什么问题?

2 个答案:

答案 0 :(得分:1)

在最后添加) AS t

SELECT 
    LAST_UPDATED_WEEK, 
    SUM( NUM_STATEMENTS ) OVER (ORDER BY LAST_UPDATED_WEEK ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) AS 'Number of account statements'
FROM (
    SELECT DATEPART(wk, STATEMENT_CREATION_DATE) AS LAST_UPDATED_WEEK,
            COUNT(DISTINCT STATEMENT_ID) AS NUM_STATEMENTS
    FROM   STATEMENTS_TABLE
    GROUP BY DATEPART(wk, STATEMENT_CREATION_DATE)) AS t

答案 1 :(得分:0)

您将order bypartition by混为一谈。 sum表达式应为:

SUM(NUM_STATEMENTS) OVER (PARTITION BY LAST_UPDATED_WEEK 
                          ROWS BETWEEN UNBOUNDED PRECEDING AND 
                                       CURRENT ROW ) AS 
"Number of account statements"