我和Oracle 11g数据库有以下三个表(简化):
IP表,包含IP标识符,IP和IP状态以及FQDN。 IP可能会重复。
+-------+-------------+-----------+-----------+
| ID_IP | IP | IP_STATUS | FQDN |
+-------+-------------+-----------+-----------+
| 1 | 192.168.1.1 | 1 | test.com |
| 2 | 192.168.1.1 | 2 | test.com |
| 3 | 192.168.1.1 | 3 | test.com |
| 4 | 10.10.45.12 | 2 | test2.com |
+-------+-------------+-----------+-----------+
VLAN表,包含VLAN标识符和VLAN编号
+---------+-------------+
| VLAN_ID | VLAN_NUMBER |
+---------+-------------+
| 1 | 3 |
| 2 | 5 |
| 3 | 7 |
+---------+-------------+
关联VLAN和IP的表:
+-------+---------+
| IP_ID | VLAN_ID |
+-------+---------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 2 |
+-------+---------+
在实际的IP表中,主键是元组(IP,IP_STATUS)。我的目标是创建一个消除IP_STATUS的新表,为此,我想聚合IP并获取VLAN_NUMBER更高的IP的ID_IP和FQDN。 SELECT查询的答案如下:
+-------+-------------+-----------+
| ID_IP | IP | FQDN |
+-------+-------------+-----------+
| 3 | 192.168.1.1 | test.com |
| 4 | 10.10.45.12 | test2.com |
+-------+-------------+-----------+
我可以使用以下查询获取IP:
SELECT i.IP, max(v.VLAN_ID)
FROM IPS i
LEFT JOIN VLAN_IP_REL v_i ON i.ID_IP=v_i.ID_IP
LEFT JOIN VLANS v ON v_i.ID_VLAN=v.ID_INSTANCIA
GROUP BY i.IP;
我不知道如何获得其他专栏。我尝试使用如下子查询:
SELECT i.ID_IP, i.IP, i.FQDN
FROM IPS i
WHERE i.IP IN (
SELECT i.IP, max(v.VLAN_ID)
FROM IPS i
LEFT JOIN VLAN_IP_REL v_i ON i.ID_IP=v_i.ID_IP
LEFT JOIN VLANS v ON v_i.ID_VLAN=v.ID_INSTANCIA
GROUP BY i.IP;
)
但它不起作用,因为子查询返回两个值,我需要max(vlan.VLAN_ID)来进行聚合。
我怎样才能获得正确的IP_ID?
谢谢!
答案 0 :(得分:2)
您可以使用分析子句按IP
拆分并按VLAN_NUMBER
排序,然后过滤以仅保留每组中的第一行:
SELECT ID_IP, IP, FQDN
FROM (
SELECT ROW_NUMBER() OVER (PARTITION BY i.IP ORDER BY v.VLAN_NUMBER DESC) AS NB,
i.ID_IP, i.IP, i.FQDN
FROM IPS i
LEFT JOIN VLAN_IP_REL v_i ON i.ID_IP = v_i.ID_IP
LEFT JOIN VLANS v ON v_i.VLAN_ID = v.VLAN_ID
) t_a
WHERE NB = 1
答案 1 :(得分:0)
您可能需要尝试 WITH 子句。大致...
file.edit()
希望这有帮助。