如何在数据大于祖先的地方获取RAW?

发布于 2025-01-31 20:31:57 字数 365 浏览 1 评论 0原文

我已经根据 number 列对表进行排序:

color - string
number - integer

我需要选择那些原始表,其中 number 的比10或更大的>颜色。

因此,例如:

color number
black 1
blue 2
black 6
black 20
black 21
blue 22
blue 23

应该返回:

black 20
blue 22

请,您能建议我如何在SQLite中编写这样的查询吗?还是至少给我一些有关如何思考问题的暗示?

I have sorted table according to number column as follows:

color - string
number - integer

I need to select those raws, where the number is by 10 or more bigger than the previous one of the same color.

So for example:

color number
black 1
blue 2
black 6
black 20
black 21
blue 22
blue 23

should return:

black 20
blue 22

Please, could you suggest me how to write such a query in SQLite? Or at least give me some hints on how to think about the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

丑疤怪 2025-02-07 20:31:57

使用lag()窗口函数以检查每行相同颜色的上一个数字:

SELECT color, number
FROM (
  SELECT *, LAG(number) OVER (PARTITION BY color ORDER BY number) prev_number
  FROM tablename
)
WHERE number - prev_number > 10;

请参阅 demo

Use LAG() window function to check the previous number of the same color for each row:

SELECT color, number
FROM (
  SELECT *, LAG(number) OVER (PARTITION BY color ORDER BY number) prev_number
  FROM tablename
)
WHERE number - prev_number > 10;

See the demo.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文