表格式如下,
ID    A     B
1     1     23
2     1     17
3     2      1
4     2      7
5     2     10
6     3     19
7     3     21
8     3      1
9     3     88要求新增一列D,D列格式如下:首先A列中的项必须相同,然后比较B列的值,B列值最小的显示最小。最后结果如下所示:ID    A     B      D
1     1     23
2     1     17     最小
3     2     1      最小
4     2     7
5     2     10
6     3     19
7     3     21
8     3     1      最小
9     3     88
请问应该怎么写语句?

解决方案 »

  1.   

    select A.*,B.D
    from tb A
    left join
    (select A,min(B) B,'最小' D from tb group by A)B
    on A.A=B.A and A.B=B.B
    ;
      

  2.   


    SELECT ID,A,B,DECODE(RN,1,'最大',NULL) D FROM
    (SELECT ROW_NUMBER() OVER(PARTITION BY A ORDER BY B) RN,ID,A,B FROM YOURTABLE)
    ORDER BY A
      

  3.   


    select * from youtable
    minus
    select * from youtable t
    exists(select 1 from  (select a,min(b)b group by a) where t.a=a and t.b=b)
    union all
    select id,a,b,'最小' d from youtable t
    exists(select 1 from  (select a,min(b)b group by a) where t.a=a and t.b=b)