- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设有一个表存储这样的层次结构:
item_id | hierarchical_id
--------+-----------------
1 | ;1;
2 | ;1;2;
3 | ;1;2;3;
4 | ;1;2;4;
5 | ;1;2;4;5;
这里存储的层次结构是 1 作为根,2 是 1 的 child ,3 和 4 是 2 的 child ,5 是 4 的 child 。
查询
SELECT
-- the substr is used to remove the first and last semicolumns
regexp_split_to_table(substr(hierarchical_id, 2, length(hierarchical_id) - 2)
, E';'
) as parent_id,
item_id,
hierarchical_id
FROM
table
返回
parent_id | item_id | hierarchical_id
----------+---------+-----------------
1 | 1 | ;1;
1 | 2 | ;1;2;
2 | 2 | ;1;2;
1 | 3 | ;1;2;3;
3 | 3 | ;1;2;3;
1 | 4 | ;1;2;3;
2 | 4 | ;1;2;4;
4 | 4 | ;1;2;4;
1 | 5 | ;1;2;4;5;
2 | 5 | ;1;2;4;5;
4 | 5 | ;1;2;4;5;
5 | 5 | ;1;2;4;5;
如何修改查询以获得第 4 列,如下所示:
parent_id | item_id | hierarchical_id | distance
----------+---------+-----------------+---------
1 | 1 | ;1; | 0
1 | 2 | ;1;2; | 1
2 | 2 | ;1;2; | 0
1 | 3 | ;1;2;3; | 2
2 | 3 | ;1;2;3; | 1
3 | 3 | ;1;2;3; | 0
1 | 4 | ;1;2;4; | 2
2 | 4 | ;1;2;4; | 1
4 | 4 | ;1;2;4; | 0
1 | 5 | ;1;2;4;5; | 3
2 | 5 | ;1;2;4;5; | 2
4 | 5 | ;1;2;4;5; | 1
5 | 5 | ;1;2;4;5; | 0
distance
的含义是当前行的item_id
和parent_id
之间的距离。例如:一个节点到它自己的距离是0,一个节点到它的父节点的距离是1,一个节点到它的父节点的距离是2等等。它不一定要从0开始。
row_number
如果我能让它在每组相等的 item_id
的 0 处重新启动,那么 row_number
会工作正常,因为 hierarchical_id
中的 id已订购。
有什么建议吗?
最佳答案
窗口函数给你很多控制;见4.2.8. Window Function Calls .
你需要的关键是:
row_number() OVER (PARTITON BY item_id ORDER BY hierarchical_id)
给定数据:
create table t ( item_id integer, hierarchical_id text );
insert into t (item_id, hierarchical_id) values
(1,';1;'),
(2,';1;2;'),
(3,';1;2;3;'),
(4,';1;2;4;'),
(5,';1;2;4;5;');
查询:
WITH x AS (
SELECT regexp_split_to_table(substr(hierarchical_id, 2, length(hierarchical_id) - 2), E';') as parent_id,
item_id,
hierarchical_id
FROM t
)
SELECT
*,
row_number() OVER (PARTITION BY item_id ORDER BY parent_id DESC) - 1 AS distance
FROM x
ORDER BY item_id, parent_id;
产生:
parent_id | item_id | hierarchical_id | distance
-----------+---------+-----------------+----------
1 | 1 | ;1; | 0
1 | 2 | ;1;2; | 1
2 | 2 | ;1;2; | 0
1 | 3 | ;1;2;3; | 2
2 | 3 | ;1;2;3; | 1
3 | 3 | ;1;2;3; | 0
1 | 4 | ;1;2;4; | 2
2 | 4 | ;1;2;4; | 1
4 | 4 | ;1;2;4; | 0
1 | 5 | ;1;2;4;5; | 3
2 | 5 | ;1;2;4;5; | 2
4 | 5 | ;1;2;4;5; | 1
5 | 5 | ;1;2;4;5; | 0
这看起来大致正确,但由于您的预期输出似乎与我运行它时提供的查询输出不匹配(第 9.1 页),所以很难确定。
关于postgresql - 类似 row_number 的方法,每组从 0 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12835025/
我正在使用 postgres 9.1 构建分页记分牌。 用户可以根据多个标准对记分板进行排序,他们可以按升序或降序排序。有一个功能可以让用户在记分牌的多个页面中找到“他们的行”,它必须反射(refle
我在练习中遇到了这个问题,我无法修复它,这让我发疯了......基本上我使用的是 SQLLite,所以我只能使用任何窗口函数来获得结果并且更痛苦的部分是已经创建的架构上没有唯一的 id。参见示例: *
语法: ROW_NUMBER() OVER([ <partition_by_clause>] <order_by_clause>) 备注: 
更新:事实上,请留下以下复杂的查询,请检查此查询。它说 Fetch 为 98%,而 Row_Number 中为 2%? Fetch 是 SQL Server 2012 的另一个营销关键字吗? ----
我需要使用以下查询向用户事件添加计数器: select PERSON_ID, TIMESTAMP, row_number() over (partition by PERSON_ID order
我有一张 table : Trip Stop Time ----------------- 1 A 1:10 1 B 1:16 1 B 1:20 1
我有以下存储过程返回 A , B , 并按降序计数。我正在尝试使用 ROW_NUMBER ,所以我可以分页记录,但我想要第一行号 1成为计数最高的记录,所以基本上,如果我返回一个包含 3 条记录的表并
在我的 sql 案例中,我在计算 row_number 的最大值时遇到了麻烦。 我将直接在 SQL Fiddle 示例上进行解释,因为我认为理解起来会更快:SQL Fiddle “OrderNumbe
我有这个运行良好的 SQL。 希望我的过滤器返回具有最高 UserSessionSequenceID 的最新唯一 SessionGuid。 问题是性能很差——即使我有很好的索引。 我怎样才能重写这个
希望有人可以在这里为我指明正确的方向。 请参阅下面的查询。 SELECT day_id,month_id,time_id, row_number() over (partition BY month_
请问函数row_number()总是以相同的方式对相同的数据进行排序? 最佳答案 不可以。SQL 中的排序不稳定,这意味着不会保留原始排序顺序。不能保证解析函数或 order by将为相同的键值以相同
我有一个用例,我需要在 PARTITION 上使用 ROW_NUMBER(): 就像是: SELECT Column1 , Column 2 ROW_NUMBER() OVER ( P
UPD:谢谢大家,话题结束, sleep 后我明白了一切=) 我在理解 OVER 子句和 ROW_NUMBER 函数时遇到问题。简单的表 - 名称和标记。我想计算每个名字的平均分数。 SELECT t
我有一个包含零的列和一个保留顺序的列,其中零表示数据片段之间的中断。这是例子 A Ord 1 1 1 2 0 3 0 4 0 5 1 6 1 7 1 8 0 9 1 10 我想要获得的是同一列,其中零
我有这个简单的查询,希望它是不言自明的。 SELECT ROW_NUMBER() OVER (PARTITION BY Price_Id ORDER BY date DESC) r, * FROM
请考虑此查询: SELECT num, * FROM ( SELECT OrderID, CustomerID, EmployeeID, OrderDate, Required
首先,我想提一下,我已经检查了所有其他提出的问题,没有一个与我的相似,所以我不认为它是重复的。 我有两个表格,“Article_tbl”到目前为止已超过 300,000 行,“ArticleZone_
我有一个非常简单的问题,但我正在使用 appInsights,并且似乎无法弄清楚如何在 aiql 或任何函数中复制 row_number() 函数(没有分区意义)。 我有综合浏览量表,我按 sessi
在 SQL 2005/2008 数据库中,我们有表 BatchMaster。列:RecordId bigint - 自动增量 id,BatchNumber bigint - 唯一非聚集索引,Batch
我正在搜索删除表中的重复条目,我看到了如下示例: CREATE TABLE Suppliers ( Id int identity (1,1), CompanyTitle nvarchar(1
我是一名优秀的程序员,十分优秀!