我需要结合两个查询的帮助。查询1查看所有节点的主列表(Nodes.Caption)。但是,所有节点都不存在请求的数据(网络流量)。我希望组合查询从InterfaceTraffic表中提取网络流量(请参阅查询1)。但是,如果此表中没有现有数据,则从VIM_VMStatistics表(查询2)中提取数据。
查询1
SELECT Caption
,Round(InAVG, 2) AS NetInAverage_Kbps
,Round(InMAX, 2) AS NetInMax_Kbps
,Round(OutAVG, 2) AS NetOutAverage_Kbps
,Round(OutMAX, 2) AS NetOutMax_Kbps
FROM (
SELECT caption
,nodeid
FROM Nodes
) n
INNER JOIN (
SELECT nodeid
,AVG(In_Averagebps) / 1024 AS InAVG
,MAX(In_Maxbps) / 1024 AS InMAX
,AVG(Out_Averagebps) / 1024 AS OutAVG
,MAX(Out_Maxbps) / 1024 AS OutMAX
FROM InterfaceTraffic
WHERE DATETIME >= DATEADD(MONTH, datediff(MONTH, 0, getdate()) - 1, 0)
AND DATETIME < DATEADD(MONTH, datediff(MONTH, 0, getdate()), 0)
GROUP BY nodeid
) l ON n.NodeID = l.NodeID
ORDER BY Caption
查询2
SELECT NAME
,Round(InAVG, 2) AS NetInAverage_Kbps
,Round(InMAX, 2) AS NetInMax_Kbps
,Round(OutAVG, 2) AS NetOutAverage_Kbps
,Round(OutMAX, 2) AS NetOutMax_Kbps
FROM (
SELECT NAME
,VirtualMachineID
,NodeID
FROM VIM_VirtualMachineNodes
) n
INNER JOIN (
SELECT VirtualMachineID
,AVG(AvgNetworkReceiveRate) AS InAVG
,MAX(MaxNetworkReceiveRate) AS InMAX
,AVG(AvgNetworkTransmitRate) AS OutAVG
,MAX(MaxNetworkTransmitRate) AS OutMAX
FROM VIM_VMStatistics
WHERE DATETIME >= DATEADD(MONTH, datediff(MONTH, 0, getdate()) - 1, 0)
AND DATETIME < DATEADD(MONTH, datediff(MONTH, 0, getdate()), 0)
GROUP BY VirtualMachineID
) l ON n.VirtualMachineID = l.VirtualMachineID
答案 0 :(得分:0)
这些可爱的小选择不需要。这将完全相同
SELECT n.Caption
,Round(InAVG, 2) AS NetInAverage_Kbps
,Round(InMAX, 2) AS NetInMax_Kbps
,Round(OutAVG, 2) AS NetOutAverage_Kbps
,Round(OutMAX, 2) AS NetOutMax_Kbps
FROM Nodes n
INNER JOIN (
SELECT nodeid
,AVG(In_Averagebps) / 1024 AS InAVG
,MAX(In_Maxbps) / 1024 AS InMAX
,AVG(Out_Averagebps) / 1024 AS OutAVG
,MAX(Out_Maxbps) / 1024 AS OutMAX
FROM InterfaceTraffic
WHERE DATETIME >= DATEADD(MONTH, datediff(MONTH, 0, getdate()) - 1, 0)
AND DATETIME < DATEADD(MONTH, datediff(MONTH, 0, getdate()), 0)
GROUP BY nodeid
) l ON n.NodeID = l.NodeID
ORDER BY Caption
SELECT n.NAME
,Round(InAVG, 2) AS NetInAverage_Kbps
,Round(InMAX, 2) AS NetInMax_Kbps
,Round(OutAVG, 2) AS NetOutAverage_Kbps
,Round(OutMAX, 2) AS NetOutMax_Kbps
FROM VIM_VirtualMachineNodes n
INNER JOIN (
SELECT VirtualMachineID
,AVG(AvgNetworkReceiveRate) AS InAVG
,MAX(MaxNetworkReceiveRate) AS InMAX
,AVG(AvgNetworkTransmitRate) AS OutAVG
,MAX(MaxNetworkTransmitRate) AS OutMAX
FROM VIM_VMStatistics
WHERE DATETIME >= DATEADD(MONTH, datediff(MONTH, 0, getdate()) - 1, 0)
AND DATETIME < DATEADD(MONTH, datediff(MONTH, 0, getdate()), 0)
GROUP BY VirtualMachineID
) l ON n.VirtualMachineID = l.VirtualMachineID
我将假设VIM_VirtualMachineNodes有一个像节点一样的nodeid字段
有很多方法可以做到这一点,这是我认为最清晰的方式......
首先,您需要一个重要的节点列表。
SELECT DISTINCT nodeid
FROM (
SELECT nodeid from nodes
UNION ALL
SELECT nodeid from VIM_VirtualMachineNodes
) as node_list
现在我们将此连接到我们的两个列表并使用coalesce选择项目
SELECT
COALESCE(l1.nodeid, l2.nodeid, 0) as nodeid,
COALESCE(l1.InAVG, l2.InAVE, 0.0) AS NetInAverage_Kbps,
COALESCE(l1.InMAX, l2.InMAX, 0.0) AS NetInMax_Kbps,
COALESCE(l1.OutAVG, l1.OutAVG, 0.0) AS NetOutAverage_Kbps,
COALESCE(l1.OutMAX, l1.OutMAX, 0.0) AS NetOutMax_Kbps
FROM (
SELECT DISTINCT nodeid
FROM (
SELECT nodeid from nodes
UNION ALL
SELECT nodeid from VIM_VirtualMachineNodes
) as node_list
) as N
LEFT JOIN (
SELECT nodeid
,AVG(In_Averagebps) / 1024 AS InAVG
,MAX(In_Maxbps) / 1024 AS InMAX
,AVG(Out_Averagebps) / 1024 AS OutAVG
,MAX(Out_Maxbps) / 1024 AS OutMAX
FROM InterfaceTraffic
WHERE DATETIME >= DATEADD(MONTH, datediff(MONTH, 0, getdate()) - 1, 0)
AND DATETIME < DATEADD(MONTH, datediff(MONTH, 0, getdate()), 0)
GROUP BY nodeid
) l1 ON n.NodeID = l1.NodeID
LEFT JOIN (
SELECT nodeid
,AVG(AvgNetworkReceiveRate) AS InAVG
,MAX(MaxNetworkReceiveRate) AS InMAX
,AVG(AvgNetworkTransmitRate) AS OutAVG
,MAX(MaxNetworkTransmitRate) AS OutMAX
FROM VIM_VMStatistics
WHERE DATETIME >= DATEADD(MONTH, datediff(MONTH, 0, getdate()) - 1, 0)
AND DATETIME < DATEADD(MONTH, datediff(MONTH, 0, getdate()), 0)
GROUP BY VirtualMachineID
) l2 ON n.NodeID = l2.NodeID
正如您所看到的,我没有打扰这个名字,但如果您愿意,可以加入子查询中的那些。
答案 1 :(得分:0)
您可以在具有左外连接的节点上对query1和query2进行查询。为简单起见,我们假设:
然后查询看起来像这样:
SELECT caption,
(CASE WHEN Q1.Caption IS NULL THEN Q2.NetInAverage_Kbps
ELSE Q1.NetInAverage_Kbps END) AS NetInAverage_Kpbs,
(CASE WHEN Q1.Caption IS NULL THEN Q2.NetInMax_Kbps
ELSE Q1.NetInMax_Kbps END) AS NetInMax_Kbps,
(CASE WHEN Q1.Caption IS NULL THEN Q2.NetOutAverage_Kbps
ELSE Q1.NetOutAverage_Kbps END) AS NetOutAverage_Kbps,
(CASE WHEN Q1.Caption IS NULL THEN Q2.NetOutMax_Kbps
ELSE Q1.NetOutAverage_Kbps END) AS NetOutMax_Kbps
FROM Nodes N
LEFT OUTER JOIN Query1 Q1 ON N.Caption=Q1.Caption
LEFT OUTER JOIN Query2 Q2 ON N.Caption=Q2.Name
此查询检查Query1中是否有行,如果没有,则使用Query2,否则使用Query1。