DB2-row_number函数-输出中需要第一个分区行

问题描述

我尝试使用SQL,但无法正常工作。

问题:它没有给出第一分区行。

我要添加表详细信息和SQL。这是针对DB2数据库的。

an illegal attempt to dereference collection

解决方法

WHERE 
    row_num >= 1 AND 
    row_num >= 2;

实际上是row_num >= 2

 WHERE 
         row_num = 1

将返回第一行

编辑: 如果您希望所有行多于1行的人-这是这样做的方法:

WITH cte_books AS (
    SELECT
        publisher_id,ROW_NUMBER() OVER (
            PARTITION BY publisher_id 
            ORDER BY rating DESC
        ) row_num,book_id,rating,title
    FROM 
        books
    WHERE 
        publisher_id IS NOT NULL
)

SELECT * 
  FROM cte_books t 
 WHERE exists (SELECT 1 
                 FROM cte_books 
                WHERE row_num > 1 
                  AND pubisher_id = t.publisher_id)
,

ROW_NUMBER()更改为COUNT(1),删除ORDER BY并使用WHERE row_num > 1

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...