test表如下:id  max_a  max_date   min_a  min_a_date
1   10    2011/01/02   5     2011/01/04
1   5    2011/01/01    4     2011/01/05
1   10   2011/01/03    4     2011/01/032   10    2011/01/02   5     2011/01/04
2   5    2011/01/01    4     2011/01/05
2   10   2011/01/03    4     2011/01/03
请问一下,我该怎样取到同一个id的最大值以及最大值对应的时间,最小值以及最小值对应的时间呢?

解决方案 »

  1.   

    select id,max_a,max_date,min_a,min_a_date
    from test t
    where not exists(select 1 from test t1 
                      where t.id=t1.id and t.max_a<t1.max_a and t.min_a>t1.min_a)
      

  2.   

    如果要和表sal表关联,就是说如果sal表中的id=test表中的id相等才求max_a,max_date,min_a,min_a_date
    该如何写呢?
    sal表如下:
    s_id
    1
    2
    3
      

  3.   

    row_number() over(PARTITION BY id ORDER BY time DESC) 
      

  4.   


    效果:
    id   max_va   max_va_date  min_va  min_va_date
    1     10       2011/01/02   4        2011/01/05
    2     10       2011/01/02   4        2011/01/05
      

  5.   


    想要的效果如下:
    id max_va max_va_date min_va min_va_date
    1 10 2011/01/02 4 2011/01/05
    2 10 2011/01/02 4 2011/01/05
    求解?