1

有没有办法在 SQL 中进行以下投影?删除第二列不根据排序更改的行?

FROM          TO
1   504     1   504
2   508     2   508
3   504     3   504
4   504     7   508
5   504     8   504
6   504     9   508
7   508     10  504
8   504     15  508
9   508     16  504
10  504         
11  504         
12  504         
13  504         
14  504         
15  508         
16  504         
17  504         
18  504         
19  504
4

2 回答 2

2

根据您使用的 RDBMS,您可以使用 LAG 和 LEAD 分析函数来查看上一行/下一行。

SELECT
  a.* 
FROM (
  SELECT
    id
  , value
  , LAG(value) OVER (ORDER BY id) previous_value
  FROM some_table
) a
WHERE a.previous_value IS NULL OR a.value != a.previous_value

在这里,内联视图将您的数据拉出,包括具有先前行值的列(按 id 排序时)。外部查询中的 WHERE 子句排除值与前一个相同的行(并确保包括显然具有 NULL previous_value 的第一行)。

于 2013-01-03T15:07:55.390 回答
1

要使用标准 SQL 执行此操作,您可以使用相关子查询。这个想法是获取以前的值,并且只保留与当前值不同的行:

select a.*
from (select t.*
             (select max(id) from t t2 where t2.id < t.id) as pevid
      from t
     ) a left outer join
     t aprev
     on a.previd = aprev.id
where aprev.value <> a.value or aprev.value is null

This is really an implementation of the lag() function, but without window functions.

You can also write this using top/limit/rownum and doing an order by in the subquery:

select a.*
from (select t.*
             (select top 1 id from t t2 where t2.id < t.id order by id desc) as pevid
      from t
     ) a left outer join
     t aprev
     on a.previd = aprev.id
where aprev.value <> a.value or aprev.value is null

This, in turn, can be simplified to remove the last join:

select a.*
from (select t.*
             (select top 1 val from t t2 where t2.id < t.id order by id desc) as pevval
      from t
     )
where a.prevval <> a.value or a.prevval is null
于 2013-01-03T15:14:55.807 回答