我试图显示特定值的最大值,以及值的相应时间戳。我让命令正常工作,但不幸的是,如果值超过一个时间段的最大值,它会显示所有时间戳。对于多个目标,这也很麻烦。以下是我现在使用的内容:
select target_name,value,collection_timestamp
from (select target_name,value,collection_timestamp,
max(value) over (partition by target_name) max_value
from mgmt$metric_details
where target_type='host' and metric_name='TotalDiskUsage'
and column_label='Total Disk Utilized (%) (across all local filesystems)'
)
where value=max_value;
我想利用相同类型的命令(试图避免内部连接等,因为缺少带宽)....但是每个target_name只显示1个最大值/时间戳。有没有办法协调一个组或限制功能,而不打破它?我对SQL有些不熟悉,所以这是全新的领域。
答案 0 :(得分:4)
您的查询非常接近。不要执行max
,而是执行row_number()
:
select target_name,value,collection_timestamp
from (select target_name,value,collection_timestamp,
row_number() over (partition by target_name order by value desc) as seqnum
from mgmt$metric_details
where target_type='host' and metric_name='TotalDiskUsage'
and column_label='Total Disk Utilized (%) (across all local filesystems)'
)
where seqnum = 1
按值分配分区中的所有内容。你想要一个最大的值,所以按降序排序并取顺序中的第一个。
答案 1 :(得分:2)
在窗口中使用ROW_NUMBER()
功能代替MAX()
和相应的ORDER BY
来解决问题:
select target_name,value,collection_timestamp
from (select target_name,value,collection_timestamp,
ROW_NUMBER() OVER (partition by target_name
ORDER BY value DESC,
collection_timestamp DESC )
AS rn
from mgmt$metric_details
where target_type='host' and metric_name='TotalDiskUsage'
and column_label='Total Disk Utilized (%) (across all local filesystems)'
)
where rn = 1 ;