我遇到了一个大问题,因为我正在尝试这样做:
[Edit]:基于@ gordon-linoff的评论,我用一个具体的例子来重塑我的问题
我必须加入两个表Sales和Quota。 表销售额:
+---------+---------+---------+---------+---------+---------+
| Country | Year | Store | Manager | Vendor | Customer|
+---------+---------+---------+---------+---------+---------+
| MX | 2018 |Cid. Mex.| Orlando | Luiz | 001 |
| MX | 2018 |Cid. Mex.| Orlando | Fabio | 002 |
| MX | 2018 |Cid. Mex.| Orlando | Luiz | 003 |
| MX | 2018 |Cid. Mex.| Orlando | Juan | 004 |
| MX | 2018 |Cid. Mex.| Orlando | Juan | 005 |
| MX | 2018 |Cid. Mex.| Javier |Hernandez| 007 |
...
配额:
+---------+---------+---------+---------+---------+---------+------------+
| Country | Year | Store | Manager | Vendor | Customer| Target |
+---------+---------+---------+---------+---------+---------+------------+
| MX | 2018 |Cid. Mex.| Orlando | Luiz | 001 | 1,01 |
| MX | 2018 |Cid. Mex.| Orlando | Fabio | | 2,00 |
| MX | 2018 |Cid. Mex.| Orlando | Luiz | | 3,05 |
| MX | 2018 |Cid. Mex.| Orlando | Juan | 004 | 2,71 |
| MX | 2018 |Cid. Mex.| Orlando | | | 14,25 |
| MX | 2018 |Cid. Mex.| | | | 16,1 |
...
最后我想要这样的事情: (销售加入配额):
| SALES FIELDS || QUOTA |
+---------+---------+---------+---------+---------+---------++------------+
| Country | Year | Store | Manager | Vendor | Customer|| Target |
+---------+---------+---------+---------+---------+---------++------------+
| MX | 2018 |Cid. Mex | Orlando | Luiz | 001 || 1,01 | *1
| MX | 2018 |Cid. Mex | Orlando | Fabio | 002 || 2,00 | *2
| MX | 2018 |Cid. Mex | Orlando | Luiz | 003 || 3,05 | *3
| MX | 2018 |Cid. Mex | Orlando | Juan | 004 || 2,71 | *4
| MX | 2018 |Cid. Mex | Orlando | Juan | 004 || 14,25 | *5
| MX | 2018 |Cid. Mex | Javier |Hernandez| 004 || 16,1 | *6
...
说明我要寻找的结果:
我认为现在更清楚了,但是如果仍然有一些阻碍理解的事情,请告诉我。
请帮助我。
预先感谢您。
答案 0 :(得分:2)
此示例可能无法完美运行,但我认为您可以通过使用多个左联接和coalesce
来完成所需的操作。试试这个,让我知道它有多近:
select
s.*,
coalesce (q1.target, q2.target, q3.target, q4.target) as target
from
sales s
left join quota q1 on
s.country = q1.country and
s.year = q1.year and
s.manager = q1.manager and
s.vendor = q1.vendor and
s.customer = q1.customer
left join quota q2 on
s.country = q2.country and
s.year = q2.year and
s.manager = q2.manager and
s.vendor = q2.vendor and
q2.customer is null
left join quota q3 on
s.country = q3.country and
s.year = q3.year and
s.manager = q3.manager and
q3.vendor is null and
q3.customer is null
left join quota q4 on
s.country = q4.country and
s.year = q4.year and
q4.manager is null and
q4.vendor is null and
q4.customer is null
本质上,您正在对同一张表进行多个联接,并尝试首先选择具有最大匹配项的联接,然后向下层叠直到找到匹配项。
答案 1 :(得分:0)
您的错误示例使任务看起来比实际要复杂:-)您想要的归结为:从我们发现的销售记录的配额匹配中,进行最精确的匹配。
在PostgreSQL中,您可以使用DISTINCT ON
执行此操作。在其他DBMS中,您可以将ROW_NUMBER
与FETCH FIRST ROW WITH TIES
等结合使用。
select distinct on (s.customer, s.country, s.vendor, s.manager, s.store, s.year)
s.*, q.target
from sales s
join quota q on (q.country = s.country or q.country is null)
and (q.year = s.year or q.year is null)
and (q.store = s.store or q.store is null)
and (q.manager = s.manager or q.manager is null)
and (q.vendor = s.vendor or q.vendor is null)
and (q.customer = s.customer or q.customer is null)
order by
s.customer, s.country, s.vendor, s.manager, s.store, s.year,
num_nonnulls(q.country, q.year, q.store, q.manager, q.vendor, q.customer) desc;