SQL JOIN值小于或等于数字

时间:2017-01-30 20:29:16

标签: sql tsql join

我正在尝试基于逻辑连接两个表,其中来自第一个表的每个记录将使用连接将值< =拉到第二个表中的数字。我很好奇是否可以在SQL中以有效的方式实现这一点。我发现了一些关注日期的问题,但没有什么相似的。

我有两张桌子。 '表1'是主要表

**Table1**
+---------+---------+--------+
| Product | Carrier | Weight |
+---------+---------+--------+
| Z       | B       | 600    |
+---------+---------+--------+
| Z       | B       | 350    |
+---------+---------+--------+
| Y       | A       | 150    |
+---------+---------+--------+
| X       | A       | 75     |
+---------+---------+--------+
| Y       | B       | 10     |
+---------+---------+--------+
| X       | A       | 40     |
+---------+---------+--------+

'表2'是查找表

**Table2**
+---------+--------+------+
| Carrier | Weight | Cost |
+---------+--------+------+
| A       | 50     | 2.50 |
+---------+--------+------+
| A       | 100    | 2.00 |
+---------+--------+------+
| A       | 200    | 1.75 |
+---------+--------+------+
| B       | 200    | 1.85 |
+---------+--------+------+
| B       | 400    | 1.50 |
+---------+--------+------+
| B       | 600    | 1.35 |
+---------+--------+------+

对于权重< = Table1

,结果将应用table2中最接近的成本
**Result**
+---------+---------+--------+------+
| Product | Carrier | Weight | Cost |
+---------+---------+--------+------+
| Z       | B       | 600    | 1.35 |
+---------+---------+--------+------+
| Z       | B       | 350    | 1.50 |
+---------+---------+--------+------+
| Y       | A       | 150    | 1.75 |
+---------+---------+--------+------+
| X       | A       | 75     | 2.00 |
+---------+---------+--------+------+
| Y       | B       | 10     | 1.85 |
+---------+---------+--------+------+
| X       | A       | 40     | 2.50 |
+---------+---------+--------+------+

使用传统联接,第一行会产生费用,因为600是表格'中列出的重量'

SELECT a.Product
      ,a.Carrier
      ,a.Weight
      ,b.Cost
FROM dbo.table1 a
LEFT JOIN Table2 b ON a.Carrier = b.Carrier AND a.Weight = b.Weight
+---------+---------+--------+------+
| Product | Carrier | Weight | Cost |
+---------+---------+--------+------+
| Z       | B       | 600    | 1.35 |
+---------+---------+--------+------+
| Z       | B       | 350    | NULL |
+---------+---------+--------+------+
| Y       | A       | 150    | NULL |
+---------+---------+--------+------+
| X       | A       | 75     | NULL |
+---------+---------+--------+------+
| Y       | B       | 10     | NULL |
+---------+---------+--------+------+
| X       | A       | 40     | NULL |
+---------+---------+--------+------+

我希望对上面的查询进行更改以达到预期的效果。

仅供参考:我使用的是Microsoft SQL Server 2014。

2 个答案:

答案 0 :(得分:2)

您应该查找符合条件的第一条记录:

SELECT a.Product
      ,a.Carrier
      ,a.Weight
      ,(select top 1 Cost from Table2 b
        where a.Carrier = b.Carrier AND a.Weight <= b.Weight
        ORDER BY b.Carrier, b.Weight ASC) Cost
FROM dbo.table1 a

答案 1 :(得分:0)

这应该让你开始:

with theCost as (
select t1.carrier
, min(t2.weight) thisWeight
from table1 t1 join table2 t2 on t1.carrier = t2.carrier
where whatever
group by t1.carrier
)
select the field you want
from theCost join table2 t2 on theCost.carrier = t2.carrier
    and thisWeight = t2.weight
etc