目标:需要对奖金项数不确定的结果集中计算每个单位的各项奖金及总奖金
说明:
奖金项表tb_item(奖金项ID,奖金项名称)
奖金信息表tb(奖金项ID,得奖单位ID,奖金额)
【奖金信息表】中的[奖金项ID]参照【奖金项表】中的[奖金项ID]目前我通过用pivot行列转换已实现了部分结果(参考辅助SQL),但不知道如何对每一行的结果进行总计,因为列数不确定。请教各位该如何修改或实现?先拜谢各位大虾了
--辅助SQL(包括数据初始化,可直接执行看结果更清晰)
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
IF OBJECT_ID('[tb_item]') IS NOT NULL DROP TABLE [tb_item]--奖金项表
create table tb_item (itemid nvarchar(50),name nvarchar(50))
insert into tb_item
select '1','A奖金' union all
select '2','B奖金' union all
select '3','C奖金'--select * from tb_item
--奖金信息表
create table tb (itemid nvarchar(50),cid nvarchar(50),amount decimal)
insert into tb
select '1','c1',50 union all
select '2','c1',30 union all
select '3','c1',20 union all
select '1','c2',25 union all
select '2','c2',10--select * from tbdeclare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + name from tb_item group by nameexec('
select * from (select x.cid,x.amount,i.name from tb x
inner join tb_item i on x.itemid=i.itemid) tb_r pivot (max(amount) for name in ('+@sql+')) t')--目前以上语句已经能实现以下结果
select 'c1' as cid,'50' as A奖金,'30' as B奖金,'20' as C奖金 union all
select 'c2','25','10',null
--需要达到的目标结果
select 'c1' as cid,'50' as A奖金,'30' as B奖金,'20' as C奖金,'100' as total union all
select 'c2','25','10','0','35'
说明:
奖金项表tb_item(奖金项ID,奖金项名称)
奖金信息表tb(奖金项ID,得奖单位ID,奖金额)
【奖金信息表】中的[奖金项ID]参照【奖金项表】中的[奖金项ID]目前我通过用pivot行列转换已实现了部分结果(参考辅助SQL),但不知道如何对每一行的结果进行总计,因为列数不确定。请教各位该如何修改或实现?先拜谢各位大虾了
--辅助SQL(包括数据初始化,可直接执行看结果更清晰)
IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb]
IF OBJECT_ID('[tb_item]') IS NOT NULL DROP TABLE [tb_item]--奖金项表
create table tb_item (itemid nvarchar(50),name nvarchar(50))
insert into tb_item
select '1','A奖金' union all
select '2','B奖金' union all
select '3','C奖金'--select * from tb_item
--奖金信息表
create table tb (itemid nvarchar(50),cid nvarchar(50),amount decimal)
insert into tb
select '1','c1',50 union all
select '2','c1',30 union all
select '3','c1',20 union all
select '1','c2',25 union all
select '2','c2',10--select * from tbdeclare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + name from tb_item group by nameexec('
select * from (select x.cid,x.amount,i.name from tb x
inner join tb_item i on x.itemid=i.itemid) tb_r pivot (max(amount) for name in ('+@sql+')) t')--目前以上语句已经能实现以下结果
select 'c1' as cid,'50' as A奖金,'30' as B奖金,'20' as C奖金 union all
select 'c2','25','10',null
--需要达到的目标结果
select 'c1' as cid,'50' as A奖金,'30' as B奖金,'20' as C奖金,'100' as total union all
select 'c2','25','10','0','35'
--奖金项表
create table tb_item (itemid nvarchar(50),name nvarchar(50))
insert into tb_item
select '1','A奖金' union all
select '2','B奖金' union all
select '3','C奖金'--select * from tb_item
--奖金信息表
create table tb (itemid nvarchar(50),cid nvarchar(50),amount decimal)
insert into tb
select '1','c1',50 union all
select '2','c1',30 union all
select '3','c1',20 union all
select '1','c2',25 union all
select '2','c2',10--select * from tbdeclare @sql varchar(8000)
select @sql = isnull(@sql + ',' , '') + name from tb_item group by name
select @sql = @sql + ',合计'exec('
select * from (select x.cid,sum(x.amount) amount,isnull(i.name,''合计'') name from tb x
inner join tb_item i on x.itemid=i.itemid group by x.cid,i.name with rollup) tb_r pivot (max(amount) for name in ('+@sql+')) t where cid is not null')drop table tb_item,tb/**********cid A奖金 B奖金 C奖金 合计
-------------------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
c1 50 30 20 100
c2 25 10 NULL 35(2 行受影响)
如果你能确定奖金项有多少个,是固定的,而且名称你都知道,那么可以直接select。
--奖金项表
create table tb_item (itemid nvarchar(50),name nvarchar(50))
insert into tb_item
select '1','A奖金' union all
select '2','B奖金' union all
select '3','C奖金'--select * from tb_item
--奖金信息表
create table tb (itemid nvarchar(50),cid nvarchar(50),amount decimal)
insert into tb
select '1','c1',50 union all
select '2','c1',30 union all
select '3','c1',20 union all
select '1','c2',25 union all
select '2','c2',10select * from
(select x.cid,sum(x.amount) amount,isnull(i.name,'合计') name
from tb x
inner join
tb_item i on x.itemid=i.itemid
group by x.cid,i.name with rollup) tb_r
pivot (max(amount) for name in (A奖金,B奖金,C奖金,合计)) t
--where cid is not null
order by cid descdrop table tb_item,tb/*************cid A奖金 B奖金 C奖金 合计
-------------------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
c2 25 10 NULL 35
c1 50 30 20 100
NULL NULL NULL NULL 135(3 行受影响)
insert into #t exec...
没有固定的列没办法直接select出来。