给出以下数据集:
输入:
awbno byhub WEIGHT
111 HO 2.50
222 HO 3.00
333 HO 2.40
444 HO 0.80
555 HO 1.00
费率列表:
<=
1. kg = 2.5卢比>
1公斤。 / 0.500克。 = 0.5卢比我正在寻找以下内容......
所需输出
AWBNO BYHUB WEIGHT AMOUNT
111 HO 2.50 4.000
222 HO 3.00 4.500
333 HO 2.40 4.000
444 HO 0.80 2.500
555 HO 1.00 2.500
答案 0 :(得分:0)
对于金额,您可以使用CASE
表达式
case when (weight - 1) <= 0 then 2.5 else 2.5 + ((weight - 1) / 0.500) * 0.5 end as amount
答案 1 :(得分:0)
试试这个并查看sqlfiddle中的demo:
select
*,
case when WEIGHT <= 1 then 2.5
else 2.5 + CEILING((WEIGHT - 1) / 0.5) * 0.5 end as AMOUNT
from yourtable
官方文档ceiling说:
返回不小于X
的最小整数值
答案 2 :(得分:0)
Use below query to get your result :
CREATE TABLE #table ( awbno INT, byhub VARCHAR(100), WEIGHT DECIMAL(12,2) ,
Amount DECIMAL(12,2))
INSERT INTO #table ( awbno , byhub, WEIGHT )
SELECT 111,'HO',2.50 UNION ALL
SELECT 222,'HO',3.00 UNION ALL
SELECT 333,'HO',2.40 UNION ALL
SELECT 444,'HO',0.80 UNION ALL
SELECT 555,'HO',1.00
UPDATE #table SET Amount = CASE WHEN (WEIGHT-1)/0.5 < 1 THEN 2.5 WHEN WEIGHT
> 1 THEN 2.5 + ( (WEIGHT-1)/0.5 * 0.5 ) END
SELECT * FROM #table