34,588
社区成员
发帖
与我相关
我的任务
分享
select b.分类,sum(a.金额) as 金额总计
from 表111 as a join 表222 as b
on a.id=b.id
group by b.分类;
if object_id('tb1','U') is not null
drop table tb1
go
create table tb1
(
id int,
金额 int
)
go
insert into tb1
select 1,12 union all
select 2,13 union all
select 3,15 union all
select 3,20 union all
select 4,12 union all
select 4,21
go
if object_id('tb2','U') is not null
drop table tb2
go
create table tb2
(
id int,
分类 varchar(10)
)
go
insert into tb2
select 1,'一类' union all
select 2,'一类' union all
select 3,'二类' union all
select 4,'二类'
go
select 分类,金额=sum(金额) from tb1 inner join tb2 on tb1.id=tb2.id group by 分类
go
/*
分类 金额
---------- -----------
二类 68
一类 25
(2 行受影响)
*/
select a.分类,sum(b.金额) from 表2 a , 表1 b where a.id=b.id group by a.分类
select a.分类,sum(b.金额)
from 表2 a , 表1 b where a.id=b.id
group by a.分类
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#T1') is null
drop table #T1
Go
Create table #T1([ID] int,[金额] int)
Insert #T1
select 1,12 union all
select 2,13 union all
select 3,15 union all
select 3,20 union all
select 4,12 union all
select 4,21
Go
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#T2') is null
drop table #T2
Go
Create table #T2([ID] int,[分类] nvarchar(2))
Insert #T2
select 1,N'一类' union all
select 2,N'一类' union all
select 3,N'二类' union all
select 4,N'二类'
Go
select
a.分类,
sum(金额) as 金额
from #T2 as a, #T1 as b
WHERE a.ID=b.ID
group by a.分类
/*
分类 金额
一类 25
二类 68
*/
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#T1') is null
drop table #T1
Go
Create table #T1([ID] int,[金额] int)
Insert #T1
select 1,12 union all
select 2,13 union all
select 3,15 union all
select 3,20 union all
select 4,12 union all
select 4,21
Go
use Tempdb
go
--> -->
if not object_id(N'Tempdb..#T2') is null
drop table #T2
Go
Create table #T2([ID] int,[分类] nvarchar(2))
Insert #T2
select 1,N'一类' union all
select 2,N'一类' union all
select 3,N'二类' union all
select 4,N'二类'
Go
select a.分类,sum(金额) as 金额 from #T2 as a inner join #T1 as b on a.ID=b.ID group by a.分类
/*
分类 金额
一类 25
二类 68
*/
select 分类,sum(金额) as 金额总计
from 表1 inner join 表2 on 表1.ID=表2.ID
group by 分类
select a.分类,sum(金额) as 金额
from 表2 as a
inner join 表1 as b on a.ID=b.ID
group by a.分类
select a.分类,sum(b.金额)
from 表2 a inner join 表1 b on a.id=b.id
group by a.分类