请高手帮忙,SQL问题

lxqlogo0 2010-06-24 06:43:09
create table #temp1
(
Flag int,
F1 decimal(18,2),
F2 decimal(18,2),
F3 decimal(18,2)
)

insert into #temp1
select 1 as Flag,2000 as F1,4000 as F2,5000 as F3
union
select 2 as Flag,360 as F1,300 as F2,400 as F3
union
select 3 as Flag,null as F1,null as F2,null as F3

select * from #temp1
drop table #temp1

想计算Flag 每列(F1,F2,F3)的值等于Flag=2的除以Flag=1的行。
即:Flag=3的行的值为:
F1:360/2000,
F2:300/4000,
F3:400/5000
请问用SQL怎么实现,谢谢!
...全文
83 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxqlogo0 2010-06-24
  • 打赏
  • 举报
回复
感谢,结贴
thinclient 2010-06-24
  • 打赏
  • 举报
回复
借了,1应改成3
update #temp1
set f1=sum(case when flag=2 then f1 else 0 end)/sum(case when flag=1 then f1 else 0 end),
f2=sum(case when flag=2 then f2 else 0 end)/sum(case when flag=1 then f2 else 0 end),
f3=sum(case when flag=2 then f3 else 0 end)/sum(case when flag=1 then f3 else 0 end)
where flag=3
thinclient 2010-06-24
  • 打赏
  • 举报
回复
update #temp1
set f1=sum(case when flag=2 then f1 else 0 end)/sum(case when flag=1 then f1 else 0 end),
f2=sum(case when flag=2 then f2 else 0 end)/sum(case when flag=1 then f2 else 0 end),
f3=sum(case when flag=2 then f3 else 0 end)/sum(case when flag=1 then f3 else 0 end)
where flag=1
chunchun0017 2010-06-24
  • 打赏
  • 举报
回复
create table #temp1
(
Flag int,
F1 decimal(18,2),
F2 decimal(18,2),
F3 decimal(18,2)
)

insert into #temp1
select 1 as Flag,2000 as F1,4000 as F2,5000 as F3
union
select 2 as Flag,360 as F1,300 as F2,400 as F3
union
select 3 as Flag,null as F1,null as F2,null as F3

select
F1=(select f1 from #temp1 where Flag=2)/(select f1 from #temp1 where Flag=1),
F2=(select f2 from #temp1 where Flag=2)/(select f2 from #temp1 where Flag=1),
F3=(select f3 from #temp1 where Flag=2)/(select f3 from #temp1 where Flag=1)
from #temp1 where Flag=3
/*
F1 F2 F3
--------------------------------------- --------------------------------------- ---------------------------------------
0.18000000000000000000 0.07500000000000000000 0.08000000000000000000

(1 行受影响)


*/
chunchun0017 2010-06-24
  • 打赏
  • 举报
回复

select
F1=(select f1 from #temp1 where Flag=2)/(select f1 from #temp1 where Flag=1),
F2=(select f2 from #temp1 where Flag=2)/(select f2 from #temp1 where Flag=1),
F3=(select f3 from #temp1 where Flag=2)/(select f3 from #temp1 where Flag=1)
from #temp1 where Flag=3
lxqlogo0 2010-06-24
  • 打赏
  • 举报
回复
计算Flag=3 的行,用Flag=2的行除以Flag=1的行
thinclient 2010-06-24
  • 打赏
  • 举报
回复
计算...的行
还是选出这些行?
不明白
thinclient 2010-06-24
  • 打赏
  • 举报
回复
没明白,顶吧

27,582

社区成员

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

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