求一条SQL语句

whiteseal 2010-06-10 04:47:32
表A
id name bankaccount
100 aaa 123000
200 bbb 321000


表B
id amount
100 200
100 310
200 450
200 540
200 600

期望得到的结果是
id name bankaccount amount
100 aaa 123000 510
200 bbb 321000 1590

...全文
136 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
极夜之辉 2010-06-11
  • 打赏
  • 举报
回复

select
a.id,a.name,a.bankaccount,sum(b.amount) amount
from
a left join b
on
a.id=b.id
lucky_2005 2010-06-11
  • 打赏
  • 举报
回复
select a.id,a.name,a.bankaccount,sum(b.amount)
from a,b
where a.id = b.id
group by a.id,a.name,a.bankaccount
xiaoxiao8372 2010-06-11
  • 打赏
  • 举报
回复
写地都挺好啊,我就不献丑了啊,哈哈!!
zhengtw 2010-06-11
  • 打赏
  • 举报
回复
select A.id, A.name, A.bankaccount, sum(B.amount) as amount
from A, B
where A.id = B.id
group by A.id, A.name, A.bankaccount
aaajedll 2010-06-11
  • 打赏
  • 举报
回复
b表GROUP BY ,SUM,然后和A表内连接就好了
宇峰科技 2010-06-11
  • 打赏
  • 举报
回复
这里的SQL 高手回答问题好快的啊
心中的彩虹 2010-06-11
  • 打赏
  • 举报
回复

if object_id('a') is not null drop table a
go
create table a(id int,name varchar(3),bankaccount int)
insert a
select 100,'aaa',123000 union all
select 200,'bbb',321000

if object_id('b') is not null drop table b
go
create table b(id int,amount int)
insert b
select 100,200 union all
select 100,310 union all
select 200,450 union all
select 200,540 union all
select 200,600

select a.id,bankaccount,sum(amount) amount
from a,b
where a.id=b.id
group by a.id,bankaccount
--结果
id bankaccount amount
100 123000 510
200 321000 1590







claro 2010-06-11
  • 打赏
  • 举报
回复
帮顶 回复内容太短了!
lxy_swufe 2010-06-10
  • 打赏
  • 举报
回复
select id,name, bankaccount ,amount
from table_A,(select table_B.id,sum(amount) from table_B group by table_B.id) table_C
where table_A.id = table_C.id;
htl258_Tony 2010-06-10
  • 打赏
  • 举报
回复
--> 生成测试数据表: [A]
IF OBJECT_ID('[A]') IS NOT NULL
DROP TABLE [A]
GO
CREATE TABLE [A] ([id] [int],[name] [nvarchar](10),[bankaccount] [int])
INSERT INTO [A]
SELECT '100','aaa','123000' UNION ALL
SELECT '200','bbb','321000'

--> 生成测试数据表: [B]
IF OBJECT_ID('[B]') IS NOT NULL
DROP TABLE [B]
GO
CREATE TABLE [B] ([id] [int],[amount] [int])
INSERT INTO [B]
SELECT '100','200' UNION ALL
SELECT '100','310' UNION ALL
SELECT '200','450' UNION ALL
SELECT '200','540' UNION ALL
SELECT '200','600'

--SELECT * FROM [A]
--SELECT * FROM [B]

-->SQL查询如下:

SELECT *
FROM a
CROSS APPLY(
SELECT SUM(amount)amount
FROM b
WHERE a.id=b.id
) c
/*
id name bankaccount amount
----------- ---------- ----------- -----------
100 aaa 123000 510
200 bbb 321000 1590

(2 行受影响)
*/
2005
ChinaITOldMan 2010-06-10
  • 打赏
  • 举报
回复
select a.id,a.name,a.bankaccount,sum(b.amount) as amount
from a,b
where a.id=b.id
group by a.id,a.name,a.bankaccount
andy_liucj 2010-06-10
  • 打赏
  • 举报
回复
select a.id,a.name,a.bankaccount, t.amount from a join (
select id,sum(amount) amount from b group by id)t on t.id=a.id
wqmgxj 2010-06-10
  • 打赏
  • 举报
回复
select a.*,c.amount
from a, (selecvt id,sum(amount) as amount from b group by id) c
where a.id=c.id
wqmgxj 2010-06-10
  • 打赏
  • 举报
回复
select a.*,c.amount
from a, (selecvt id,sum(amount) as amount from b group by id) c
where a.id=c.id
cxmcxm 2010-06-10
  • 打赏
  • 举报
回复
select a.*,c.amount
from a, (selecvt id,sum(amount) as amount from b group by id) c
where a.id=c.id
dawugui 2010-06-10
  • 打赏
  • 举报
回复
select a.* , sum(b.amount) amount from a , b where a.id = b.id group by a.id , a.name , a.bankaccount

select a.* , (select sum(b.amount) from b where b.id = a.id) amount from a
wujinyuan 2010-06-10
  • 打赏
  • 举报
回复

select a.*,t.amount from a left join (select b.ID,amount=sum(b.amount) from b group by b.ID) t on a.ID=b.id 
YangWenChaoX 2010-06-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 josy 的回复:]

SQL code
---测试数据---
if object_id('[A]') is not null drop table [A]
go
create table [A]([id] int,[name] varchar(3),[bankaccount] int)
insert [A]
select 100,'aaa',123000 union all
select 200,'bbb',321……
[/Quote]
甚好 ! 每次都提供测试数据。。。
百年树人 2010-06-10
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[A]') is not null drop table [A]
go
create table [A]([id] int,[name] varchar(3),[bankaccount] int)
insert [A]
select 100,'aaa',123000 union all
select 200,'bbb',321000
if object_id('[B]') is not null drop table [B]
go
create table [B]([id] int,[amount] int)
insert [B]
select 100,200 union all
select 100,310 union all
select 200,450 union all
select 200,540 union all
select 200,600

---查询---
select a.id,a.name,a.bankaccount,sum(b.amount) as amount
from a,b
where a.id=b.id
group by a.id,a.name,a.bankaccount

---结果---
id name bankaccount amount
----------- ---- ----------- -----------
100 aaa 123000 510
200 bbb 321000 1590

(2 行受影响)
永生天地 2010-06-10
  • 打赏
  • 举报
回复

select
a.id,a.name,a.bankaccount,sum(b.amount) amount
from 表A a left join 表B b on a.id=b.id group by a.id
加载更多回复(1)

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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