sql-server - 窗口函数的参数应该是分区常量?

标签 sql-server oracle

下面的SQL

with t as (
    select Id
    from IA
    INNER JOIN IAD ON IAD.PKEY = IA.DID
    LEFT JOIN  IAC ON IAC.INS = IA.INS
)
select Id,
       ntile((select count(*) from t) / 5000) over ( order by Id ) NT
from t;

在Oracle中运行时出现以下错误。

ORA-30488: argument should be a function of expressions in PARTITION BY
30488. 00000 -  "argument should be a function of expressions in PARTITION BY"
*Cause:    The argument of the window function should be a constant for
           a partition.
*Action:
Error at Line: 66 Column: 20

How to assign the parameter of NTile with computed value? I will need the code be able to be run in SQL Server via linked server.

-- SQL Server
select * from openquery(oracledb, '....the code above...')

最佳答案

函数中对“参数应该是 PARTITION BY 中表达式的函数”的要求有点奇怪。 类似这样的事情:

with t as (
     select Id, 
            trunc ( count(*) over ()  / 500) as cnt
       from IA
      INNER JOIN IAD ON IAD.PKEY = IA.DID
       LEFT JOIN IAC ON IAC.INS = IA.INS
 )
 select Id,
        ntile(cnt ) over (partition by cnt order by Id ) NT
 from t;

关于sql-server - 窗口函数的参数应该是分区常量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46577377/

相关文章:

java - 为什么请求无效转换错误代码 : 17132?

java - 如何从 Java 中的 txt 文件中读取 sql 查询

sql - 从文本 regexp_substr 中删除第二个引号

sql-server - 为什么SQL Server在合并连接后添加排序

sql-server - 从查询中选择第 n 个结果

sql - 为什么使用 phone = N'123 4' slower than phone = ' 1234' 查询?

database - 使用 :new and :old referencing different tables in SQL

sql - 如何连接这些表以获得所有表的完整结果集?

.net - DataReader 基于名称的查找 - 到底发生了什么,导致它们比循环中基于序数的查找慢得多?

c# - Singleton 上的 StackOverflowException