22,207
社区成员
发帖
与我相关
我的任务
分享
create table table_c
(
fname varchar(80),
ftype varchar(80),
fvalue float
)
insert into table_c values('张三','奖金','500')
insert into table_c values('李四','奖金','400')
insert into table_c values('张三','应发工资','2500')
insert into table_c values('李四','应发工资','2400')
select fname,ftype,fvalue from table_c
union all
select a.fname,ftype='百分比',fvalue=round(b.fvalue/a.fvalue,3) from
(select fname,ftype,fvalue from table_c where ftype='应发工资') a
inner join
(select fname,ftype,fvalue from table_c where ftype='奖金') b on a.fname=b.fname
if object_id('tb')>0
drop table tb
create table tb
(
fname varchar(20),
ftype nvarchar(20),
fvalue int
)
insert into tb
select '张三', '奖金', 500
union all
select '李四', '奖金', 400
union all
select '张三', '应发工资', 2500
union all
select '李四', '应发工资', 2400
select * from tb
union all
select b1.fname,'百分比',cast(b1.fvalue*1.0/b2.fvalue*1.0 as decimal(19,2)) fvalue
from tb b1
join tb b2
on b1.fname=b2.fname
where b1.ftype='奖金' and b2.ftype='应发工资'
fname ftype fvalue
-------------------- -------------------- ---------------------------------------
张三 奖金 500.00
李四 奖金 400.00
张三 应发工资 2500.00
李四 应发工资 2400.00
张三 百分比 0.20
李四 百分比 0.17
(6 行受影响)
if object_id('tb')>0
drop table tb
create table tb
(
fname varchar(20),
ftype nvarchar(20),
fvalue int
)
insert into tb
select '张三', '奖金', 500
union all
select '李四', '奖金', 400
union all
select '张三', '应发工资', 2500
union all
select '李四', '应发工资', 2400
select fname,ftype,cast(fvalue as numeric(10,2)) from tb
union all
select fname,'百分比',
cast(sum(case when ftype ='奖金' then fvalue else 0 end)*1.0/sum(fvalue) as numeric(10,2))
from tb
group by fname
if object_id('tb')is not nulll
drop table tb
create table tb
(
fname varchar(20),
ftype nvarchar(20),
fvalue int
)
insert into tb
select '张三', '奖金', 500
union all
select '李四', '奖金', 400
union all
select '张三', '应发工资', 2500
union all
select '李四', '应发工资', 2400
select * from tb
union all
select a.fname,'百分比' as ftype,cast(a.fvalue*1.0/b.fvalue*1.0 as decimal(19,2))as fvalue
from tb a, tb b
where a.ftype='奖金' and b.ftype='应发工资' and a.fname=b.fname