1
lichao 2014-10-17 10:57:14 +08:00 1
select a, b, case when a/b > 0.5 then 5 when a/b > 0.3 then 4 when a/b > 0.12 then 3 else 0 end as score
|
4
tobyzw 2014-10-17 11:17:33 +08:00 1
select a, b, case when a/b > 0.5 then 5 when a/b > 0.3 then 4 when a/b > 0.12 then 3 else 0 end as score
------------------------- 可能会有问题,a/b可能会出异常,b=0的情况需要考虑进去 |
5
CosWind OP @lichao 诶。我测试的结果是80w的表,这样写和a/b设置成常量1计算出来的结果所需要的时间分别是1.01s 和 0.85s,差别还是有的
|
6
xudshen 2014-10-17 11:19:32 +08:00 1
用a > 0.5*b 会不会效率高一点?
|
13
frye 2014-10-17 11:43:43 +08:00
SELECT
a, b, IF( a / b > 0.12, IF( a / b > 0.18, IF( a / b > 0.3, 5, 4 ), 3 ), 0 ) AS score FROM table_name |
14
cye3s 2014-10-17 11:50:06 +08:00 via Android 1
子查询查一次a/b,外面套case
|
15
frye 2014-10-17 11:52:20 +08:00 1
INSERT INTO TEST_A (account_id, profit_rate, score) SELECT
account_id, IF ( init_funds > 0, profit / init_funds, 0 ) AS profit_rate, IF ( IF ( init_funds > 0, profit / init_funds, 0 ) > 0.12, IF ( IF ( init_funds > 0, profit / init_funds, 0 ) > 0.18, IF ( IF ( init_funds > 0, profit / init_funds, 0 ) > 0.3, 5, 4 ), 3 ), 0 ) AS score FROM TEST_B |
18
frye 2014-10-17 11:57:14 +08:00 1
INSERT INTO TEST_A (account_id, profit_rate, score) SELECT
TEST_B.account_id, TEST_B.profit_rate, IF ( TEST_B.profit_rate > 0.12, IF ( TEST_B.profit_rate > 0.18, IF (TEST_B.profit_rate > 0.3, 5, 4), 3 ), 0 ) AS score FROM ( SELECT IF ( init_funds > 0, profit / init_funds, 0 ) AS profit_rate, account_id FROM TEST_B ) AS TEST_B |
20
viquuu 2014-10-17 12:05:15 +08:00 1
select a,b,
case when c > 0.5 then 5 when c > 0.3 then 4 when c > 0.12 then 3 else 0 end as score from ( select a,b, a/isnull(b,1) as c from test ) |