SQL使用N-1 Dense Rank过滤掉密集秩N中的行 Mysql

SQL using N-1 Dense Rank to filter out rows in dense rank N Mysql(SQL使用N-1 Dense Rank过滤掉密集秩N中的行 Mysql)
本文介绍了SQL使用N-1 Dense Rank过滤掉密集秩N中的行 Mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的标题不清楚,这里是我所说的一个例子.

If my title isn't clear here's an example of what I am talking about.

假设我有一张如下所示的表格:

Let's say I have a table that looks like this:

+----+------+----+----+---------+-------+---------+-------+
| ID | Time | X  | Y  | X_START | X_END | Y_START | Y_END |
+----+------+----+----+---------+-------+---------+-------+
|  1 |    0 |  8 |  6 |       6 |    10 |       4 |     8 |
|  2 |    0 | 20 | 10 |      18 |    22 |       8 |    12 |
|  3 |    1 |  8 |  8 |       6 |    10 |       6 |    10 |
|  4 |    1 | 10 | 24 |       8 |    12 |      22 |    26 |
+----+------+----+----+---------+-------+---------+-------+

如果我将此查询应用于表:

If I apply this query to the table:

WITH 
cte1 AS (SELECT *,
    DENSE_RANK() OVER (ORDER BY TIME) AS DENSE_RANK
    FROM data
    ORDER BY TIME)

SELECT * FROM cte1

我明白了:

+----+------+----+----+---------+-------+---------+-------+------------+
| ID | Time | X  | Y  | X_START | X_END | Y_START | Y_END | DENSE_RANK |
+----+------+----+----+---------+-------+---------+-------+------------+
|  1 |    0 |  8 |  6 |       6 |    10 |       4 |     8 |          1 |
|  2 |    0 | 20 | 10 |      18 |    22 |       8 |    12 |          1 |
|  3 |    1 |  8 |  8 |       6 |    10 |       6 |    10 |          2 |
|  4 |    1 | 10 | 24 |       8 |    12 |      22 |    26 |          2 |
+----+------+----+----+---------+-------+---------+-------+------------+

现在我要做的是过滤掉 X 在 X_START - X_END 范围内且 Y 在 Y_START - Y_END 范围内且 DENSE_RANK 为 n-1 的任何行

Now what I want to do is filter out any rows where X is within the range X_START - X_END AND Y is within the range Y_START - Y_END AND DENSE_RANK is n-1

所以我想要一个看起来像这样的结果:

so I'd like a result that looks like this:

+----+------+----+----+---------+-------+---------+-------+------------+
| ID | Time | X  | Y  | X_START | X_END | Y_START | Y_END | DENSE_RANK |
+----+------+----+----+---------+-------+---------+-------+------------+
|  1 |    0 |  8 |  6 |       6 |    10 |       4 |     8 |          1 |
|  2 |    0 | 20 | 10 |      18 |    22 |       8 |    12 |          1 |
|  4 |    1 | 10 | 24 |       8 |    12 |      22 |    26 |          2 |
+----+------+----+----+---------+-------+---------+-------+------------+

我对 SQL 很陌生,所以我不太确定如何去做.提前感谢您的所有帮助!

I am pretty new to SQL so I am not too sure on how to go about this. Thank you for all help in advance!

推荐答案

使用MIN()窗口函数来识别每个Time的最小ID代码> 以便在所有其他条件也满足的情况下排除该行:

Use MIN() window function to identify the minimum ID for each Time so that you can exclude that row if all the other conditions are satisfied too:

WITH cte AS (
    SELECT *, 
           DENSE_RANK() OVER (ORDER BY TIME) AS `DENSE_RANK`,
           MIN(ID) OVER (PARTITION BY TIME) min_id
    FROM data
  )
SELECT ID, Time, X, Y, X_START, X_END, Y_START, Y_END, `DENSE_RANK` 
FROM cte
WHERE `DENSE_RANK` = 1 
  OR NOT (ID = min_id AND X BETWEEN X_START AND X_END AND Y BETWEEN Y_START AND Y_END)
ORDER BY `DENSE_RANK`, ID;

请参阅演示.

这篇关于SQL使用N-1 Dense Rank过滤掉密集秩N中的行 Mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)