在SQL Server的WHERE子句中使用ROW_NUMBER()

时间:2019-04-11 10:09:40

标签: sql sql-server tsql

我在Microsoft SQL Server中有此查询:

SELECT *
FROM
(SELECT 0 run_id,
      0 tran_id,
      0 sort_id,
      ' ' tran_type,
          10 prod_id,
          72 type_id,
          1 value,
            CONVERT(DATETIME ,'2016-03-18 00:00:00', 102) + row_number() OVER (ORDER BY (SELECT NULL)) -1 + convert(varchar(8), sw.end_time, 8) event_publication,
          DATEPART (YEAR
                   FROM (CONVERT(DATETIME, '2016-03-18 00:00:00', 102) + row_number() OVER (ORDER BY (SELECT NULL)) -1)) y,
                  DATEPART (MONTH
                           FROM (CONVERT(DATETIME, '2016-03-18 00:00:00', 102) + row_number() OVER (ORDER BY (SELECT NULL)) -1)) mo,
                          DATEPART (DAY
                                   FROM (CONVERT(DATETIME, '2016-03-18 00:00:00', 102) + row_number() OVER (ORDER BY (SELECT NULL)) -1)) d,
                                  datepart (hour from sw.end_time) h,
                                  datepart (minute from sw.end_time) mi,
                                  datepart (second from sw.end_time) s,
                                  0 ms
FROM sys.all_objects ao,
    settlement_win sw,
    prod_def pd
WHERE pd.prod_id = 10
 AND sw.country = pd.country
 AND sw.commodity = pd.commodity
 AND row_number() OVER (ORDER BY (SELECT NULL)) <= CONVERT(DATETIME, '2016-03-18 23:59:00', 102) -CONVERT(DATETIME, '2016-03-18 00:00:00', 102)+1
)AS A WHERE  1=1
AND event_publication >= CONVERT(VARCHAR(19), '2016-03-18 00:00:00', 20)
AND event_publication <= CONVERT(VARCHAR(19), '2016-03-18 23:59:00', 20)
ORDER BY run_id,
     prod_id,
     type_id,
     y,
     mo,
     d,
     h,
     mi,
     s,
     ms,
     sort_id,
     tran_id,
     value;

在Microsoft SQL Server Management Studio中执行它时,出现此错误:

  

消息4108,第15层,状态1,第27行

     

窗口函数只能出现在SELECT或ORDER BY子句中。

由于行AND row_number() OVER (ORDER BY (SELECT NULL)) <= CONVERT(DATETIME, '2016-03-18 23:59:00', 102) -CONVERT(DATETIME, '2016-03-18 00:00:00', 102)+1,我也找不到一种适用于我的查询的解决方法。

1 个答案:

答案 0 :(得分:1)

您不能在where子句中使用窗口功能,但可以在子查询或cte的下一层进行检查

    SELECT *
    FROM
    (SELECT 0 run_id,
          0 tran_id,
          0 sort_id,
          ' ' tran_type,
              10 prod_id,
              72 type_id,
              1 value,
                CONVERT(DATETIME ,'2016-03-18 00:00:00', 102) + row_number() OVER (ORDER BY (SELECT NULL)) -1 + convert(varchar(8), sw.end_time, 8) event_publication,
              DATEPART (YEAR
                       FROM (CONVERT(DATETIME, '2016-03-18 00:00:00', 102) + row_number() OVER (ORDER BY (SELECT NULL)) -1)) y,
                      DATEPART (MONTH
                               FROM (CONVERT(DATETIME, '2016-03-18 00:00:00', 102) + row_number() OVER (ORDER BY (SELECT NULL)) -1)) mo,
                              DATEPART (DAY
                                       FROM (CONVERT(DATETIME, '2016-03-18 00:00:00', 102) + row_number() OVER (ORDER BY (SELECT NULL)) -1)) d,
                                      datepart (hour from sw.end_time) h,
                                      datepart (minute from sw.end_time) mi,
                                      datepart (second from sw.end_time) s,
                                      0 ms
   ,row_number() OVER (ORDER BY (SELECT NULL)) as rn
   FROM sys.all_objects ao,
        settlement_win sw,
        prod_def pd
    WHERE pd.prod_id = 10
     AND sw.country = pd.country
     AND sw.commodity = pd.commodity

    )AS A WHERE  1=1
    AND event_publication >= CONVERT(VARCHAR(19), '2016-03-18 00:00:00', 20)
    AND event_publication <= CONVERT(VARCHAR(19), '2016-03-18 23:59:00', 20)
    AND  rn <= CONVERT(DATETIME, '2016-03-18 23:59:00', 102) -CONVERT(DATETIME, '2016-03-18 00:00:00', 102)+1
    ORDER BY run_id,
         prod_id,
         type_id,
         y,
         mo,
         d,
         h,
         mi,
         s,
         ms,
         sort_id,
         tran_id,
         value;