遇到很多 sql 代碼when:
case
when callDuration > 0 and callDuration < 30 then 1.4
when callDuration >= 30 and callDuration < 60 then 2.3
when callDuration >= 60 and callDuration < 120 then 3.7
when callDuration >= 120 and callDuration < 180 then 4.5
when callDuration >= 180 and callDuration < 240 then 5.2
when callDuration >= 240 and callDuration < 300 then 6.1
when callDuration >= 300 and callDuration < 360 then 7.3
when callDuration >= 360 and callDuration < 420 then 8.4
when callDuration >= 420 and callDuration < 480 then 9.2
when callDuration >= 480 and callDuration < 540 then 10.1
when callDuration >= 540 and callDuration < 600 then 11.9
when callDuration >= 600 then 12.3
end as duration
如果有100行這種when和then,怎么簡化更優雅,我可以考慮用Jinjia Template或者用lookup table。有沒有更好的方法,不受特定變體的限制?
uj5u.com熱心網友回復:
方法
我認為最優雅的解決方案是查找表(您在上面提到過)。
下面是一個示例,但為簡單起見,我沒有輸入示例中列出的所有范圍。
創建資料
create table lookupTable(
startCallDuration int,
endCallDuration int,
returnValue float);
insert into lookupTable values (0,30,1.4);
insert into lookupTable values (30,60,2.3);
insert into lookupTable values (60,120,3.7);
insert into lookupTable values (120,999999999,4.5);
create table callDuration(
callDuration int );
insert into callDuration values (30);
insert into callDuration values (60);
陳述句
select returnValue from lookupTable l, callDuration c
where c.callDuration >= startCallDuration and
c.callDuration < endCallDuration;
SQL陳述句(內連接)
select returnValue from lookupTable l inner join callDuration c
on c.callDuration >= startCallDuration and
c.callDuration < endCallDuration;
SQLFiddle:
輸出是

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/534367.html
標籤:Google Cloud Collective 数据库数据库数据库谷歌大查询案子
上一篇:回傳從未相互交手過的球隊-SQL
