我的表格有三列 id , time_stamp , vm_name , cpu_usage 。
该表没有每个时间戳的所有vm_names的值。
但是我想要一个结果,其中包含每个time_stamp的每个vm_name的值。
我的表有4个独特的虚拟机和20个独特的时间戳。 并且该表包含50行。
我希望返回4 X 20 = 80行,如果vm_name和time_stamp的组合尚未存在,则会为cpu使用分配0值。
下面的尝试返回162行,其中我没有看到cpuusage值不存在的任何空值。
select t1.timestamp,t2.timestamp,t1.vm_name,t2.vm_name,t1.cpuUsage, t2.cpuUsage from `VM_monitoring_5min t1`
right join VM_monitoring_5min t2
on t1.timestamp = t2.timestamp
这是SQL小提琴http://sqlfiddle.com/#!2/998e0/1
答案 0 :(得分:0)
交叉连接所有不同的vm_name和时间戳,然后左连接你的表。
select n.vm_name,c.timestamp,coalesce(t1.cpuUsage,0)
from
(select distinct vm_name from VM_monitoring_5min) as n
cross join
(select distinct timestamp from VM_monitoring_5min) as c
left join
VM_monitoring_5min t1
on n.vm_name = t1.vm_name and c.timestamp = t1.timestamp
;
答案 1 :(得分:0)
select distinct n.vm_name
, c.timestamp
, coalesce(t1.cpuUsage,0)
from VM_monitoring_5min n
join VM_monitoring_5min c
left
join VM_monitoring_5min t1
On t1.vm_name = n.vm_name
and t1.timestamp = c.timestamp