我有一个sql,如下:
select case when ((A.a-B.b)/A.a)<0.2 then 0 when ((A.a-B.b)/A.a)<0.5 then 0.5 else 1 end as aa
from A,B
where A.id=b.id
请问这个语句可以简化吗?
A,B是表,a,b,id是字段。
select case when ((A.a-B.b)/A.a)<0.2 then 0 when ((A.a-B.b)/A.a)<0.5 then 0.5 else 1 end as aa
from A,B
where A.id=b.id
请问这个语句可以简化吗?
A,B是表,a,b,id是字段。
from A,B
( @a1 float, @a2 float )
returns int as
begin
declare @n = float
set @n = (@a1 - @a2) / @a1
if @n < .2 return 0
if @n < .5 return .5
return 1
end
go
select dbo.fn_level(A.a, B.b) from A, B where A.id = B.id
select case when (1-B.b/A.a)<0.2 then 0 when (1-B.b/A.a)<0.5 then 0.5 else 1 end as aa
from A,B
where A.id=b.id
如果能保证 b字段在A表中没有 ,a字段在B表中没有,下面写的可以写的更少些,
select case when (1-b/a)<0.2 then 0 when (1-b/a)<0.5 then 0.5 else 1 end as aa
from A,B
where A.id=b.id
只是 参与 本质没变。
无法简化了!