请教如何进行单项分属不同项目的分组查询?

chifung 2006-04-03 09:26:49
表结构:
mytype nvarchar(2)
myint int(8)

实例:
R1:'AB', 100
R2: 'A', 200
R3: 'B', 100

说明:
R1中的100表示既属于A,也属于B
R2中的200表示仅属于A
R3中的100表示仅属于B

问题:
需要列出分组合计的结果如下:
A, 300
B, 200

A系R1+R2
B系R2+R3
均指myint列相加。
...全文
240 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
OYGX 2006-09-25
  • 打赏
  • 举报
回复
学习一下
zhaotianyi 2006-04-04
  • 打赏
  • 举报
回复
--下面是测试结果
create table #mytable
(
mytype varchar(10),
myint int
)
insert into #mytable values('a',100)
insert into #mytable values('b',800)
insert into #mytable values('ab',100)

select
mytype,
(
select sum(myint) from #mytable t2
where t2.mytype like '%'+t1.mytype+'%'
) as myintSum
from #mytable t1
where len(mytype)=1
drop table #mytable
zhaotianyi 2006-04-04
  • 打赏
  • 举报
回复
select
mytype,
(
select sum(myint) from mytable t2
where t2.mytype like '%'+t1.mytype+'%'
) as myintSum
from mytable t1
where len(mytype)=1

27,581

社区成员

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

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