比如一个数据表:
x    y
0    -10
1     -5
2     -1
3      1
4      8
5      5
6      -2
7      -3
现在我要查出x=2,x=6
知道(2,6)之间y值最大处x=4,需要查出x=4左右与y值相反的第一个x值
即x=4往左边符号第一次相反时x=2;x=4往右边符号第一次相反时x=6(可以在x,y轴上画画)
求这个sql语句谢谢了

解决方案 »

  1.   

    select *
    from (select *,row=row_number()over(order by y desc) from table1 where x between 2 and 6)t
    where row=1
      

  2.   

    或select * 
    from table1  AS a
    where x between 2 and 6
    AND NOT EXISTS(SELECT 1 FROM table1 WHERE x between 2 and 6 AND y>a.y)
      

  3.   

    select
     * 
    from
     table1 t
    where
     x between 2 and 6
    and
     not exists(select 1 from table1 where x between 2 and 6 and y>t.y)
      

  4.   

    x=2,x=6是要查出来的结果
    已知条件是x=4,y=8,即x=4时,y值最大