Sql 按线程路径和总投票数排序注释
我在按线程路径和每条注释的向上投票数排序注释时遇到一些问题 现在它们只按线程路径排序。我已经尝试和搜索了很多东西,但都没有结果 这是我的问题Sql 按线程路径和总投票数排序注释,sql,postgresql,Sql,Postgresql,我在按线程路径和每条注释的向上投票数排序注释时遇到一些问题 现在它们只按线程路径排序。我已经尝试和搜索了很多东西,但都没有结果 这是我的问题 WITH RECURSIVE first_comments AS ( ( ( SELECT id, text, level, parent_id, array[id] AS thread_path, total_votes FROM comments WHERE comments."postId" = 1 AND comments."leve
WITH RECURSIVE first_comments AS (
(
(
SELECT id, text, level, parent_id, array[id] AS thread_path, total_votes FROM comments
WHERE comments."postId" = 1 AND comments."level" = 0
)
)
UNION
(
SELECT e.id, e.text, e.level, e.parent_id, (fle.thread_path || e.id), e.total_votes
FROM
(
SELECT id, text, level, parent_id, total_votes FROM comments
WHERE comments."postId" = 1
) e, first_comments fle
WHERE e.parent_id = fle.id
)
)
SELECT id, text, level, total_votes, thread_path from first_comments ORDER BY 5 ASC
此查询结果为:
--------------------------------------------------
| id | level | total_votes | thread_path |
--------------------------------------------------
| 1 | 0 | 5 | {1} |
| 3 | 1 | 9 | {1,3} |
| 7 | 2 | 5 | {1,3,7} |
| 9 | 2 | 7 | {1,3,9} |
| 11 | 3 | 0 | {1,3,9,11} |
| 12 | 4 | 0 | {1,3,9,11,12} |
| 13 | 5 | 0 | {1,3,9,11,12,13} |
| 10 | 1 | 20 | {1,10} |
| 2 | 0 | 10 | {2} |
| 6 | 1 | 1 | {2,6} |
| 4 | 0 | 8 | {4} |
| 8 | 1 | 6 | {4,8} |
| 5 | 0 | 3 | {5} |
--------------------------------------------------
并且结果应该是
--------------------------------------------------
| id | level | total_votes | thread_path |
--------------------------------------------------
| 2 | 0 | 10 | {2} |
| 6 | 1 | 1 | {2,6} |
| 4 | 0 | 8 | {4} |
| 8 | 1 | 6 | {4,8} |
| 1 | 0 | 5 | {1} |
| 10 | 1 | 20 | {1,10} |
| 3 | 1 | 9 | {1,3} |
| 9 | 2 | 7 | {1,3,9} |
| 11 | 3 | 0 | {1,3,9,11} |
| 12 | 4 | 0 | {1,3,9,11,12} |
| 13 | 5 | 0 | {1,3,9,11,12,13} |
| 7 | 2 | 5 | {1,3,7} |
| 5 | 0 | 3 | {5} |
--------------------------------------------------
我错过了什么
感谢您的帮助您想按最高级别的总票数排序。我想我将通过使用窗口函数来实现这一点 而不是:
SELECT id, text, level, total_votes, path
from first_comments
ORDER BY 5 ASC;
它显式地按路径排序。试试这个:
select id, text, level, total_votes,
max(total_votes) over (partition by path[1]) as toplevel_votes
from first_comments
order by 6 desc;
这将计算最高级别的总投票数,并将其用于排序。只需在路径旁边累积另一个数组,该数组将不仅包含其路径中每个注释的
id
,还包含每个id之前的total_voces
(作为负数)。之后,您可以按该列排序
WITH RECURSIVE first_comments AS (
(
(
SELECT id, text, level, parent_id, array[id] AS path, total_votes,
array[-total_votes, id] AS path_and_votes
FROM comments
WHERE comments."postId" = 1 AND comments."level" = 0
)
)
UNION
(
SELECT e.id, e.text, e.level, e.parent_id, (fle.path || e.id), e.total_votes,
(fle.path_and_votes || -e.total_votes || e.id)
FROM
(
SELECT id, text, level, parent_id, total_votes FROM comments
WHERE comments."postId" = 1
) e, first_comments fle
WHERE e.parent_id = fle.id
)
)
SELECT id, text, level, total_votes, path from first_comments ORDER BY path_and_votes ASC
(仅数据—不带递归CTE)我删除了MySQL标签,因为MySQL不支持递归CTE。你不应该按4,5或5,4排序吗?我必须承认,我没有看到搜索结果集中的进度。我想我明白了:按路径结果排序回复在注释之后,即“回复到”。但是你想通过
total_vows
?@Gordon,谢谢你,但是pozs确实做到了。还有一个问题,你觉得这个标题合适吗?我发现搜索这些术语非常困难