Mysql 如何仅在(SQL)项的顺序递增时才选择它们?
我在SQL中有一个表,如下所示:Mysql 如何仅在(SQL)项的顺序递增时才选择它们?,mysql,sql,Mysql,Sql,我在SQL中有一个表,如下所示: -------------------------- |ID | number| numberDate | |---|-------|------------- | 1 | 120 | 2011-01-22 | |---|-------|------------| | 1 | 124 | 2011-01-27 | |---|-------|------------| | 2 | 136 | 2011-01-20 | |---|-------|----
--------------------------
|ID | number| numberDate |
|---|-------|-------------
| 1 | 120 | 2011-01-22 |
|---|-------|------------|
| 1 | 124 | 2011-01-27 |
|---|-------|------------|
| 2 | 136 | 2011-01-20 |
|---|-------|------------|
| 2 | 135 | 2011-01-30 |
|---|-------|------------|
| 3 | 150 | 2011-01-15 |
|---|-------|------------|
| 3 | 155 | 2011-01-19 |
|---|-------|------------|
| 3 | 180 | 2011-01-23 |
--------------------------
我想选择数量不断增加的ID。在上面的示例中,我会选择ID1和ID3,因为:对于ID1,我们有120可以使用where exists…子查询
此查询应为您提供所有编号不断增加的id:
select distinct t.id from table_name t
where exists (
select t2.number
from table_name t2
where t2.id=t.id
and t2.number > t.number
)
我们将分两步进行
--Step 1: Find records the violate the rule
With BadIDs AS (
--IDs where there is another record with a matching ID and lower number, but greater date
select t1.id
from [table] t1
inner join [table] t2 on t2.id = t1.id
where t1.number > t2.number and t1.numberDate < t2.numberDate
)
-- Step 2: All IDs not part of the first step:
select distinct ID from [table] WHERE ID NOT IN (select ID from BadIDs)
不幸的是,MySql不支持CTEs公共表表达式。以下是一个可用于MySql的版本:
select distinct ID
from [table]
WHERE ID NOT IN
(
select t1.id
from [table] t1
inner join [table] t2 on t2.id = t1.id
where t1.number > t2.number and t1.numberDate < t2.numberDate
)
我承认我不确定MYSQL是否会处理这些分析函数,但是我相信它们应该处理。 不过,我在Oracle上进行了测试。。所以你可能需要一些小的调整 w_数据视图只是模仿您的数据。。。 然后,它通过两种方法对数据进行排序:按日期和按值。 然后比较这两个订单。。如果他们一路匹配,你就没事了。如果有什么不对劲的话。。然后在那里的某个地方,这些值就减少了
with w_data as (
select 1 id, 120 cnumber, to_date('2011-01-22','yyyy-mm-dd') numDate from dual union all
select 1 id, 124 cnumber, to_date('2011-01-27','yyyy-mm-dd') numDate from dual union all
select 2 id, 136 cnumber, to_date('2011-01-20','yyyy-mm-dd') numDate from dual union all
select 2 id, 135 cnumber, to_date('2011-01-30','yyyy-mm-dd') numDate from dual union all
select 3 id, 150 cnumber, to_date('2011-01-15','yyyy-mm-dd') numDate from dual union all
select 3 id, 155 cnumber, to_date('2011-01-19','yyyy-mm-dd') numDate from dual union all
select 3 id, 180 cnumber, to_date('2011-01-23','yyyy-mm-dd') numDate from dual union all
select 4 id, 150 cnumber, to_date('2011-01-15','yyyy-mm-dd') numDate from dual union all
select 4 id, 145 cnumber, to_date('2011-01-19','yyyy-mm-dd') numDate from dual union all
select 4 id, 180 cnumber, to_date('2011-01-23','yyyy-mm-dd') numDate from dual
),
w_check as (
select id, cnumber, numDate,
row_number() over (partition by id order by numDate) ordDate,
row_number() over (partition by id order by cnumber) ordNum,
count(*) over (partition by id) cnt
from w_data
)
select id
from w_check
where ordDate = ordNum
group by id, cnt
having count(*) = cnt
/
结果:
ID
----------
1
3
2 rows selected.
什么列表示记录的实际顺序?SQL表表示无序集。没有下一行或上一行的意义,除非您有一个定义该顺序的列。您是否有一个id列或创建日期以及相应的顺序?我有一个名为numberDate的列,对应的id在其中放置了数字。您想要同时使用1还是将它们分组?@Kira请通过编辑将该列数据添加到您的问题中,并根据更新的示例数据添加预期的输出。我发誓mysql不在标记中当我第一次写这篇文章时,我就列出了它,但我知道它现在就在那个里,而且它总是修订历史的一部分。。。所以,是的,这不适用于MySql,因为MySql不支持CTE。我会马上更新一个版本。。。
ID
----------
1
3
2 rows selected.