- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下是我的查询:
SELECT *
FROM (
SELECT f.max, f.min, p.user_id, p.id, p.title, p.rating,
RANK() OVER (
PARTITION BY p.user_id
ORDER BY p.rating DESC, p.id DESC
) AS rnk
FROM posts AS p
INNER JOIN friends AS f ON (p.user_id = f.friend_id)
WHERE f.user_id=1
) AS subq
WHERE (subq.rnk <= subq.max)
LIMIT 10
它搜索我 friend 的帖子,按他们的评分和日期排序。通过此查询中实现的窗口函数,我可以根据 Friends
表上的 MAX
字段限制为每个 friend 返回的行数。
但是,我还有一个字段MIN
,它用于指定我想要从给定 friend 的查询中获得的最小帖子数。这怎么可能?
我还想知道 SQL 是否是这些类型查询的最佳选择?我已经尝试过 Neo4j 图形数据库,虽然它看起来是一个很好的解决方案,但我宁愿避免使用 2 个单独的数据库。
架构:
CREATE TABLE friends(
user_id int,
friend_id int,
min int,
max int
);
CREATE TABLE posts(
id int,
title varchar(255),
rating int,
date date,
user_id int
);
假设我们有以下数据:
INSERT INTO friends VALUES
(1,2,1,3)
, (1,3,0,5)
, (1,4,2,10);
INSERT INTO posts VALUES
(1, 'posts1', 2, now(), 2)
, (2, 'posts2', 1, now(), 2)
, (3, 'posts3', 5, now(), 2)
, (4, 'posts4', 2, now(), 2)
, (5, 'posts5', 11, now(), 2)
, (6, 'posts6', 7, now(), 2)
, (7, 'posts7', 3, now(), 2)
, (8, 'posts8', 4, now(), 3)
, (9, 'posts9', 1, now(), 3)
, (10, 'posts10', 0, now(), 3)
, (11, 'posts11', 7, now(), 3)
, (12, 'posts12', 3, now(), 3)
, (13, 'posts13', 2, now(), 3)
, (14, 'posts14', 4, now(), 4)
, (15, 'posts15', 9, now(), 4)
, (16, 'posts16', 0, now(), 4)
, (17, 'posts17', 3, now(), 4)
, (18, 'posts18', 2, now(), 4)
, (19, 'posts19', 1, now(), 4)
, (20, 'posts20', 2, now(), 4);
因此,如果可能的话,我希望看到(post_id, title, rating, date,friend_id)
组合具有以下条件:
id
=2 的好友发布了 1 到 3 条帖子id
=3 的好友发布了 0 到 5 条帖子id
=4 的好友发布了 2 到 10 条帖子基本上,如果我的 friend friend_id=2
发布了 1 篇或多篇文章,我至少想要其中 2 篇。如果他发表的文章超过 3 篇,我想要的文章不超过 3 篇。
最佳答案
Let's say I want to have 2-5 posts from you everyday, if you post that much. If you post only one, it is alright, and I will have the only one post.
您的解释in the comment仍然没有加起来。根据此解释,您的 min
数字将是噪音,没有任何效果。
这不是你写的,但这很有意义:
给定帖子的最大显示位置(外部 LIMIT
),我想首先从每个 friend 那里获取 min
个帖子(如果有) )。如果此后还有空位,请为每个 friend 最多填充 max
个帖子。
在此示例中,好友 2 发布的 1 (min
) 条帖子具有最高优先级,如果还有更多空位,则另外 2 (max - min
) 条帖子也是如此。
如果每个优先级没有足够的空位,那么哪些帖子被淘汰将是任意的。我继续假设应该首先选择每个帖子的第一篇文章,等等。
其余的仍然是任意的,但如果你设法制定一个要求,就可以轻松解决。
SELECT *
FROM friends f
, LATERAL (
SELECT *
, row_number() OVER (ORDER BY rating DESC NULLS LAST, id DESC) AS rn
FROM posts p
WHERE user_id = f.friend_id -- LATERAL reference
ORDER BY rating DESC NULLS LAST, date DESC NULLS LAST
LIMIT f.max -- LATERAL reference
) p
WHERE f.user_id = 1
ORDER BY (p.rn > f.min) -- minimum posts from each first
, p.rn
LIMIT 10; -- arbitrary total maximum
假设 friends.user_id
和 posts.id
为主键。那里缺少您的表定义。
所有其他列都应定义为 NOT NULL
才有意义。
使用 LATERAL
连接仅选择子查询中每个 friend 的 max
个帖子:
使用row_number()
, not rank()
在子查询中。混淆两者是一个常见的错误。
您提到了日期
,但它没有显示在您的查询中。也许你真的想要:
, row_number() OVER (ORDER BY rating DESC NULLS LAST
, date DESC NULLS LAST) AS rn
DESC NULLS LAST
仅因为评级
和日期
可能为NULL:
在 Postgres 中,您可以在 ORDER BY
中使用简单的 bool 表达式:
ORDER BY (p.rn > f.min), p.rn
这会将每个 friend 的 min
个帖子放在第一位。第二项 (p.rn
) 为每个 friend 提供平等的机会(先发帖先等等)。
不要使用
作为标识符。它是标准 SQL 中的保留字,也是 Postgres 中的基本类型名称。日期
关于sql - 每组的最小行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30001333/
我有一个包含未定义条目数的数据文件,如下所示: A B C D E.. 1 0 2 5 4 7 4 3 4 1 8 7 4 0 7 1 1 第一行代表工作时间,而不是暂停等交替方式。为了可
我需要有关小型 SQL 查询的帮助。考虑下表: TicketNo | Rules | Audit Result --------------------------------- P
我有一个非常大的表(~1 000 000 行)和带有联合、连接和 where 语句的复杂查询(用户可以选择不同的 ORDER BY 列和方向)。我需要获取分页的行数。如果我运行查询而不计算行数,它会很
我想获取数据帧的行数。 我可以通过 size(myDataFrame)[1] 实现这一点. 有更干净的方法吗? 最佳答案 如果您正在使用 DataFrames具体来说,那么你可以使用 nrow() :
是否可以在带有千位分隔符的 VIM 状态栏中显示行数,最好是自定义千位分隔符? 例子: set statusline=%L 应该导致“1,234,567”而不是“1234567”。 最佳答案 我找到了
我有一个非常基本的问题,但不知道该怎么做。如果 mysql 表中的行数增加,我想刷新页面。我已经尝试了一些不同的事情,比如在表中添加一个单独的列,如果行数和这个值相等,则值为 (id + 1),然后进
我的 mysql TB 中的行数(如 TB 信息中所示)是 11093,而自动递增 ID(从 1 开始)是 11361。为什么会这样? 最佳答案 删除的行不会重置 AI 索引。行数是当前表中的条目数,
我有一个 MySQL 表如下。 emp_no emp_name dob gender 1 A 1978-10-10 Male 2 B
ifstream inFile; inFile.open(filename); //open the input file stringstream strStream; strStream << i
SELECT * FROM table1 WHERE EXISTS (SELECT * FROM table2 WHERE *condition*) 例如,我可以检查是否有 3 行符合 table2
我正在尝试提取 SQL 表中的总行数。 我正在使用以下代码: $rowNum = mysql_query("SELECT COUNT(*) FROM Logs"); $count = mysql_fe
我想知道表格 View 的行宽是多少,UITableViewCell 文本标签的字体是什么,有人可以帮我吗? 最佳答案 NSLog(@"width: %f", cell.frame.size.widt
对于以下内容: def linecount(filename): count = 0 for x in open(filename): count += 1 r
感谢关注。 我用C语言写了一段代码来统计字数、行数和字符数。 while((c = fgetc(fp)) != EOF) { if((char)(c) == ' ' || (char)(c)
我是 matlab 的新手,只需要更改代码中的一个非常小的东西。我有以下矩阵: ans = 1 1 1 1 2 1 2 1
我只是想弄清楚如何确定行数,然后使该数字显示在 HTML 中。 我准备好的声明如下所示: if($stmt = $mysqli -> prepare("SELECT field1, field2, f
PDO 显然无法计算从选择查询返回的行数(mysqli 有 num_rows 变量)。 除了使用 count($results->fetchAll()) 之外,有没有办法做到这一点? 最佳答案 根据手
SELECT count(*) FROM Stack WHERE Id = 33478 GROUP BY SID Output: (No column name) 1 4 对于结果;有两排。怎么退货
IE。如果我们有一个包含400万行的表。 其中具有一个STATUS字段,该字段可以采用以下值:TO_WORK,BLOCKED或WORKED_CORRECTLY。 您是否会在一个仅会更改一次的字段上进行
所以在JTextArea中有一个getLineCount()是否有与JTextPane类似的东西,因为我可以找到任何东西。也许有不同的方法来获得它?我想获取当前存在的行数。 最佳答案 (正如您所指出的
我是一名优秀的程序员,十分优秀!