--try
select 帐单号=帐单号,结款方式名称=case when 结帐金额
=max(case when 帐单号=帐单号 then  结帐金额 end) then 结帐方式名称 end
from 结帐方式表
group by 帐单号

解决方案 »

  1.   

    --测试环境
    declare @t table(帐单号 varchar(10),结帐方式编码 varchar(10),结帐方式名称 varchar(10),结帐金额 money)
    insert into @t select '10001','01','现金',55.00
    union all select '10001','02','支票',0.00
    union all select '10001','03','会员卡',10.00
    union all select '10002','01','现金',0.00
    union all select '10002','02','支票',500.00
    union all select '10002','03','会员卡',0.00
    --查询
    select 帐单号,结帐方式名称=(select 结帐方式名称 from @t where 帐单号=a.帐单号 and 结帐金额=a.结帐金额)
    from
    (
    select 帐单号=帐单号,结帐金额=max(case when 帐单号=帐单号 then  结帐金额 end) 
    from @t
    group by 帐单号
    ) a--结果
    帐单号        结帐方式名称     
    ---------- ---------- 
    10001      现金
    10002      支票(所影响的行数为 2 行)
      

  2.   

    declare @t table(帐单号 varchar(10),结帐方式编码 varchar(10),结帐方式名称 varchar(10),结帐金额 money)
    insert into @t select '10001','01','现金',55.00
    union all select '10001','02','支票',0.00
    union all select '10001','03','会员卡',10.00
    union all select '10002','01','现金',0.00
    union all select '10002','02','支票',500.00
    union all select '10002','03','会员卡',0.00
    --查询
    select 帐单号
           ,结帐方式名称
    from @t t
    where 结帐金额=(select max(结帐金额) from @t where 帐单号=t.帐单号)
    group by 帐单号,结帐方式名称
    order by 帐单号--结果
    /*
    帐单号        结帐方式名称     
    ---------- ---------- 
    10001      现金
    10002      支票(所影响的行数为 2 行)
    */