edgar108 2012-11-13 13:48 采纳率: 100%
浏览 168
已采纳

这个sql怎么写?

这个sql怎么写?

test表,有time,num,type  3个字段,表的数据类似是这样

time          num    type
时间1      1        A
时间2      1    A
时间3      1   A
时间4      1   B
时间5      1   B
时间6              1   B
时间7 1   B

时间8 2   C
时间9 2   C
时间10 2   C
时间11 2   C
时间12 2   D
时间13   2   D
时间14 2   D

以上数据时间是倒序排列的,即时间1最新,时间14最老,

现在要把同一个num的type发生改变时的前后2条记录查出来,
即对应上面的:

时间3 1   A
时间4 1   B
时间11 2   C
时间12 2   D

查出这4条记录,sql应该怎么写? 数据库是Oracle。

 

  • 写回答

1条回答 默认 最新

  • iteye_7115 2012-11-13 15:28
    关注

    [code="sql"]
    select time,num,type from (
    select
    time,num,type,
    lag(type) over(partition by num order by time desc) lag_type,
    lead(type) over(partition by num order by time desc) lead_type
    from t
    ) a
    where a.lag_type is not null
    and a.lead_type is not null
    and (a.type <> a.lag_type or a.type <> a.lead_type)
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退