我需要创建一个像这样的数据结构:
Table 1
Code, Value, Offer_ID
我正在创建一项服务,对于给定的“代码”和“值”组合,该服务必须返回我预先配置的Offer_ID。
例如:
Code Value Offer_ID
------ ------- ----------
Age 30 OFF1
Age 30 OFF2
Province RM OFF2
Age 40 OFF3
Province TO OFF3
Age 40 OFF4
Province TO OFF4
Operator TIM OFF4
呼叫服务部门总是打电话给我传递“年龄”,“省”和“话务员”值。 如果我为三个值(如OFF4)或2(如OFF3)或年龄(唯一的强制性(OFF1))找到了一个特定的Offer_ID,则必须查看此表。
所以如果客户通过我省BO和操作员WIND,我必须返回OFF1
我该怎么办?如何构造表和查询?
我希望我能解决这个问题...
感谢1000名帮助我的人...我们快要疯了... !!!
答案 0 :(得分:0)
尝试一下:
with tab (age, province, operator, offer_id) as (values
(30, null, null, 'OFF1')
, (30, 'RM', null, 'OFF2')
, (40, 'TO', null, 'OFF3')
, (40, 'TO', 'TIM', 'OFF4')
)
, op_inp (age, province, operator) as (values
--(40, 'TO', 'TIM') --'OFF4'
(40, 'TO', 'VODAFONE') --'OFF3'
--(30, 'RM', 'VODAFONE') --'OFF2'
--(30, 'TO', 'VODAFONE') --'OFF1'
)
select offer_id /*Just for info*/, order_flag
from
(
select t.*, 3 as order_flag
from tab t
join op_inp o on o.age=t.age and o.province=t.province and o.operator=t.operator
union all
select t.*, 2 as order_flag
from tab t
join op_inp o on o.age=t.age and o.province=t.province --and t.operator is null
union all
select t.*, 1 as order_flag
from tab t
join op_inp o on o.age=t.age --and t.province is null and t.operator is null
)
order by order_flag desc
fetch first 1 row only
;