[英]Select multiple columns based on the max of another column
I have and Oracle 11g Database with the following three tables (simplified): 我已经和Oracle 11g数据库一起使用了以下三个表(简化了):
IP Table, containing an IP identifier, the IP, and IP status and a FQDN. IP表,包含IP标识符,IP和IP状态以及FQDN。 IPs might be repeated.
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 Table, containing and VLAN identifier and the VLAN number VLAN表,包含和VLAN标识符以及VLAN号
+---------+-------------+
| VLAN_ID | VLAN_NUMBER |
+---------+-------------+
| 1 | 3 |
| 2 | 5 |
| 3 | 7 |
+---------+-------------+
A Table correlating VLANs and IPs: 关联VLAN和IP的表:
+-------+---------+
| IP_ID | VLAN_ID |
+-------+---------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
| 4 | 2 |
+-------+---------+
In the actual IP Table, the primary key is the tuple (IP, IP_STATUS). 在实际的IP表中,主键是元组(IP,IP_STATUS)。 My goal is to create a new table eliminating the IP_STATUS, and to do that, I want to aggregate IPs and get the ID_IP and FQDN of the IP whose VLAN_NUMBER is higher.
我的目标是创建一个消除IP_STATUS的新表,为此,我想聚合IP并获取其VLAN_NUMBER更高的IP的ID_IP和FQDN。 The answer for the SELECT query would be something like this:
SELECT查询的答案将是这样的:
+-------+-------------+-----------+
| ID_IP | IP | FQDN |
+-------+-------------+-----------+
| 3 | 192.168.1.1 | test.com |
| 4 | 10.10.45.12 | test2.com |
+-------+-------------+-----------+
I can get the IP using the following query: 我可以使用以下查询获取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;
What I don't know is how to get the other columns. 我不知道如何获得其他专栏。 I tried with a subquery like the following:
我尝试了如下子查询:
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;
)
But it doesn't work, since the subquery returns two values, and I need the max(vlan.VLAN_ID) to do the aggregation. 但这是行不通的,因为子查询返回两个值,并且我需要max(vlan.VLAN_ID)进行聚合。
How could I get the right IP_ID? 如何获得正确的IP_ID?
Thank you! 谢谢!
You can use an analytical clause to split by IP
and order by VLAN_NUMBER
, then filter to retain only the first line in each group : 您可以使用分析子句按
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
You may want try WITH clause. 您可能需要try WITH子句。 Roughly...
大致...
WITH IPWITHMAXVLANID(IP, MAXVLAN) AS (
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, iml.MAXVLAN
FROM IPS i
INNER JOIN IPWITHMAXVLANID iml on i.IP = imp.IP
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.