select 单位编码,max(口径)as 口径,max(水量)as 水量,用水性质=(case when 用水性质='生产' then '生产' else '生活' end) from tablename group by 单位编码

解决方案 »

  1.   

    create table sb (单位编码 char (10),水表代码 int,口径 int,水量 int ,用水性质 char (4))insert into sb 
    select     'A0101',    12,        15,      100 ,    '生活'
    union 
    select     'A0101',    13 ,       16   ,   110   ,  '生产'
    union
    select     'A0105',    14,        22,      120,     '生活'
    union 
    select     'A0105',    15   ,     22 ,     130,     '生活'
    union
    select     'A0106' ,  14 ,       22  ,    120  ,   '生活'
    union
    select     'A0106',    15  ,      22 ,     120 ,    '生产'
    select b.*,a.用水性质 from sb a,(
    select 单位编码  ,max(水表代码) 水表代码,max (口径) as 口径,max (水量) as 水量
    from sb
    group by 单位编码) b
    where a.单位编码=b.单位编码 and a.水表代码=b.水表代码单位编码  水表代码   口径    水量    用水性质   
    --------  -------   ----    ----    -------
    A0101    13        16      110     生产
    A0105    15        22      130     生活
    A0106    15        22      120     生产
      

  2.   

    select 单位编码,max(水表代码),max(口径),max(水量),max(用水性质)from table1 group by 单位编码在音序中,生产>生活。所以max(用水性质)优先选生产。
      

  3.   


    楼上的加一条这个就不对了啊 
    insert into sb 
    select     'A0106',    15  ,      22 ,     120 ,    '生活'
      

  4.   

    成这样了
    单位编码  水表代码     口径       水量       用水性质 
    ---------- ----------- ----------- ----------- ---- 
    A0101      13          16          110         生产
    A0105      15          22          130         生活
    A0106      15          22          120         生产
    A0106      15          22          120         生活
      

  5.   

    这样对了吧..
    select 单位编码,max(水表代码) as 水表代码,max(口径) as 口径,max(水量) as 水量,min(用水性质) as 用水性质
     from sb group by 单位编码结果:
    单位编码   水表代码    口径       水量       用水性质 
    ---------- ----------- ----------- ----------- ---- 
    A0101      13          16          110         生产
    A0105      15          22          130         生活
    A0106      15          22          120         生产bernice99(bernice99)错了,应该是min(用水性质)