--try
select 帐单号=帐单号,结款方式名称=case when 结帐金额
=max(case when 帐单号=帐单号 then 结帐金额 end) then 结帐方式名称 end
from 结帐方式表
group by 帐单号
select 帐单号=帐单号,结款方式名称=case when 结帐金额
=max(case when 帐单号=帐单号 then 结帐金额 end) then 结帐方式名称 end
from 结帐方式表
group by 帐单号
解决方案 »
- 查询12个月数据,没有数据的月份生成为0
- 如何在一表中求和值
- 关于MS-SQL的查询语句问题,请指教!
- 在存储过程中,能否在select的from后的表名,使用@tab这样的变量
- MSSQL 查询EXCEL的问题
- 这个查询能不能改得快一点,或者怎样设置索引好点??
- excel b d f 列的用|||链接如何写excel公式
- 请问客户端程序连接不到服务端的数据库时原因?
- [DENETLTB][ConnectionOpen(Connect()).]SQL Server不存在或拒绝访问???
- 送100分,请教如何建立一个与某已存在表同结构的表?
- 如何检查数据库中有没有某个视图,如果有就删掉重建,如果没有就新建?
- 这样的动态统计该怎么做,请各位指点指点,我是一个新手,谢谢了!
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 行)
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 行)
*/