如何选择(SQL)项目,只有它们按递增顺序排列?

时间:2015-03-24 18:44:28

标签: mysql sql

我在SQL中有一个表,如下所示:

--------------------------
|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。在上面的示例中,我将选择ID 1和ID 3,因为:对于ID 1,我们有120 <124而对于ID 3,我们有150 <155 <180。

输出应为:

-----
|ID | 
|---|
| 1 |
|---|
| 3 |
-----

我无法理解。

感谢。

编辑:我添加了第三列,并提供了一些示例输出。

3 个答案:

答案 0 :(得分:2)

我们分两步完成。

--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不支持CTE(公用表表达式)。这是一个适用于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
 )

答案 1 :(得分:0)

您可以使用where exists...subquery

此查询应该为您提供所有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
)

答案 2 :(得分:0)

我承认不确定MYSQL是否会处理这些分析功能,但我相信他们应该这样做。 我在Oracle上测试了这个,但是..所以你可能需要一些小的调整......

(w_data视图只是模仿你的数据...) 然后它通过两种方法对数据进行排序:按日期和按值。 然后它比较那两个订单..如果它们一直匹配,你就可以了。如果有什么东西没有排成一行......那么某些地方的价值就会减少。

  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.