日期:2014-05-16  浏览次数:20541 次

sql 合并字段
问题:怎么将如下图的三行合并为一行数据

------解决方案--------------------
试试这个:
select ID,vip_accounts,
       MAX(THprices) THprices,
       MAX(SendGoodsPrice) SendGoodsPrice,
       MAX(balanceMoney)  balanceMoney
from T_isok_order_ALL_Account
where vip_accounts='素素'
group by ID,vip_accounts

------解决方案--------------------
分组group by vip_accounts
------解决方案--------------------

select id,vip_accounts,isnull(THprices,0)+isnull(SendGoodsPrice,0)+isnull(balanceMoney,0) from 表


数值型的可以直接加和
------解决方案--------------------
修改一下:


select id,vip_accounts,max(isnull(THprices,0)) as THprices,max(isnull(SendGoodsPrice,0)) as SendGoodsPrice max(isnull(balanceMoney,0))) as balanceMoney   from 表 group by id,vip_accounts




------解决方案--------------------
引用:
Quote: 引用:

试试这个:
select ID,vip_accounts,
       MAX(THprices) THprices,
       MAX(SendGoodsPrice) SendGoodsPrice,
       MAX(balanceMoney)  balanceMoney
from T_isok_order_ALL_Account
where vip_accounts='素素'
group by ID,vip_accounts


结果还是一样


我看了,原来是id不同,虽然vip_accounts相同,那么这个id是5,8,13一共有3个,这个怎么处理
------解决方案--------------------



select min(id)as id,vip_accounts,max(isnull(THprices,0)) as THprices,max(isnull(SendGoodsPrice,0)) as SendGoodsPrice max(isnull(balanceMoney,0))) as balanceMoney   from 表 group by vip_accounts






------解决方案--------------------
select min(ID) id,
       vip_accounts,
       MAX(THprices) THprices,
       MAX(SendGoodsPrice) SendGoodsPrice,
       MAX(balanceMoney)  balanceMoney
from T_isok_order_ALL_Account  group by vip_accounts

------解决方案--------------------
引用:
Quote: 引用:




select min(id)as id,vip_accounts,max(isnull(THprices,0)) as THprices,max(isnull(SendGoodsPrice,0)) as SendGoodsPrice max(isnull(balanceMoney,0))) as balanceMoney   from 表 group by vip_accounts

按照你这方法,还是有问题,负数是无法出来的









修改一下,把isnull放到max外面,就可以了:
select min(id)as id,vip_accounts,isnull(max(THprices),0) as THprices,isnull(max(SendGoodsPrice),0)