SQL 两列中有相同的值,怎么合并起来

[复制链接]
查看11 | 回复4 | 2010-6-13 20:07:15 | 显示全部楼层 |阅读模式
例如:
表A
a
sl
b
ssl
1班 5
NULL
NULL
2班 NULL 2班
2
3班 1
3班
1
输出为
1班 5
2班 2
3班 2

回复

使用道具 举报

千问 | 2010-6-13 20:07:15 | 显示全部楼层
select (case when a is null then b else a end) as class,((case when sl is null then 0 else sl end) + (case when ssl is null then 0 else ssl end)) as amoutfrom A
回复

使用道具 举报

千问 | 2010-6-13 20:07:15 | 显示全部楼层
select A.a,nvl(sl,0)+nvl(b,0)+nvl(ssl,0) amt from A
回复

使用道具 举报

千问 | 2010-6-13 20:07:15 | 显示全部楼层
select a,sum(isnull(sl,0)) as 合计from(select a,slfrom 表Awhere a is not nullunion allselect b,sslfrom 表Awhere b is not null) tb
回复

使用道具 举报

千问 | 2010-6-13 20:07:15 | 显示全部楼层
select case when a is null then b else a end as 班级,(isnull(sl,0) + isnull(ssl,0)) as 总数量from 表A
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行