sql 合并字段

慕容引刀 2014-02-25 10:02:15
问题:怎么将如下图的三行合并为一行数据
...全文
216 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
慕容引刀 2014-02-25
  • 打赏
  • 举报
回复
引用 15 楼 yupeigu 的回复:
[quote=引用 14 楼 a1871837908 的回复:] [quote=引用 9 楼 u010192842 的回复:]



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

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







[/quote] 修改一下,把isnull放到max外面,就可以了:
select min(id)as id,vip_accounts,isnull(max(THprices),0) as THprices,isnull(max(SendGoodsPrice),0) as SendGoodsPrice,isnull(max(balanceMoney),0) as balanceMoney   from 表 group by vip_accounts
[/quote] 嗯嗯 是可以了,谢谢啦
LongRui888 2014-02-25
  • 打赏
  • 举报
回复
引用 14 楼 a1871837908 的回复:
[quote=引用 9 楼 u010192842 的回复:]



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

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







[/quote] 修改一下,把isnull放到max外面,就可以了:
select min(id)as id,vip_accounts,isnull(max(THprices),0) as THprices,isnull(max(SendGoodsPrice),0) as SendGoodsPrice,isnull(max(balanceMoney),0) as balanceMoney   from 表 group by vip_accounts
慕容引刀 2014-02-25
  • 打赏
  • 举报
回复
引用 9 楼 u010192842 的回复:



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

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







慕容引刀 2014-02-25
  • 打赏
  • 举报
回复
引用 10 楼 yupeigu 的回复:
[quote=引用 8 楼 a1871837908 的回复:] [quote=引用 6 楼 yupeigu 的回复:] [quote=引用 5 楼 a1871837908 的回复:] [quote=引用 1 楼 yupeigu 的回复:] 试试这个:
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
结果还是一样[/quote] 我看了,原来是id不同,虽然vip_accounts相同,那么这个id是5,8,13一共有3个,这个怎么处理[/quote] 其实我这个表的三个数据字段,是其他三张表合并过来的[/quote] 哦 这个没关系的, 现在的关键是图中的3条数据,id值不同,那么要合并3条数据,就必须要确定这个id值如何处理,是随机选一个,还是哪个呢,比如这个是选最大值:
select max(ID) 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
[/quote] 嗯 谢谢你的思路,不过需要在max()函数里面放isnull() 其实我也是刚刚知道的 哈哈
t101lian 2014-02-25
  • 打赏
  • 举报
回复
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
慕容引刀 2014-02-25
  • 打赏
  • 举报
回复
引用 9 楼 u010192842 的回复:



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





可以了 你好棒哦 我去好好研究下
LongRui888 2014-02-25
  • 打赏
  • 举报
回复
引用 8 楼 a1871837908 的回复:
[quote=引用 6 楼 yupeigu 的回复:] [quote=引用 5 楼 a1871837908 的回复:] [quote=引用 1 楼 yupeigu 的回复:] 试试这个:
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
结果还是一样[/quote] 我看了,原来是id不同,虽然vip_accounts相同,那么这个id是5,8,13一共有3个,这个怎么处理[/quote] 其实我这个表的三个数据字段,是其他三张表合并过来的[/quote] 哦 这个没关系的, 现在的关键是图中的3条数据,id值不同,那么要合并3条数据,就必须要确定这个id值如何处理,是随机选一个,还是哪个呢,比如这个是选最大值:
select max(ID) 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
Yole 2014-02-25
  • 打赏
  • 举报
回复



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





慕容引刀 2014-02-25
  • 打赏
  • 举报
回复
引用 6 楼 yupeigu 的回复:
[quote=引用 5 楼 a1871837908 的回复:] [quote=引用 1 楼 yupeigu 的回复:] 试试这个:
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
结果还是一样[/quote] 我看了,原来是id不同,虽然vip_accounts相同,那么这个id是5,8,13一共有3个,这个怎么处理[/quote] 其实我这个表的三个数据字段,是其他三张表合并过来的
慕容引刀 2014-02-25
  • 打赏
  • 举报
回复
引用 4 楼 u010192842 的回复:
修改一下:


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



也不行,只是把 “null” 变成 “0” 了
LongRui888 2014-02-25
  • 打赏
  • 举报
回复
引用 5 楼 a1871837908 的回复:
[quote=引用 1 楼 yupeigu 的回复:] 试试这个:
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
结果还是一样[/quote] 我看了,原来是id不同,虽然vip_accounts相同,那么这个id是5,8,13一共有3个,这个怎么处理
慕容引刀 2014-02-25
  • 打赏
  • 举报
回复
引用 1 楼 yupeigu 的回复:
试试这个:
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
结果还是一样
Yole 2014-02-25
  • 打赏
  • 举报
回复
修改一下:


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



Yole 2014-02-25
  • 打赏
  • 举报
回复

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

数值型的可以直接加和
快溜 2014-02-25
  • 打赏
  • 举报
回复
分组group by vip_accounts
LongRui888 2014-02-25
  • 打赏
  • 举报
回复
试试这个:
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

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧