查询以匹配多个范围中的值

时间:2015-04-08 15:28:14

标签: sql ms-access

我有一张表格如下:

promo_code  |   minimum_order   |   discount
------------+-------------------+-------------
100         |   10              |   10%
100         |   20              |   15%
100         |   30              |   20%
101         |   13              |   7%
102         |   8               |   10%
102         |   12              |   14%

在另一张表中,我有数量的销售额和符合条件的促销活动

sales   |   promo_eligibility   |   record_id
--------+-----------------------+-------------
14      |   100                 |   1000
7       |   101                 |   1001
25      |   102                 |   1002

我需要获得与销量和促销相对应的折扣......

如上例所示:

record_id   |   discount    |   Comments
------------+---------------+--------------
1000        |   15%         |   (bigger than 10 and lower than 20)
1001        |   0%          |   (did not reached the minimum)
1002        |   14%         |   

阈值的数量可以在0到3之间变化

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

这样的事情:

select 
  r.record_id,
  isnull(d.discount, '0%')
from records r
outer apply (
  select top 1 discount
  from discounts d
  where d.promo_code = r.promo_eligibility
  and d.minimum_order <= r.sales)
相关问题