我有一张桌子,如上图所示。对于各种系统及其资源类型,我有行。 RAM,HDD,CPU等。另外,对于每种资源类型,我有一个实际上是(RevisionId)&的列R_ID。每次更新时递增1。
示例:下面我的列数很少
(Machinename->资源 - > R_Id-> MemorySizeInGb)
我想要的是获得每台机器的最高R_ID记录的MemorySizeInGb
。所以从上面来看,我想要的输出将是(请注意,我只需要尺寸,下面只是为了清楚地说明事情)。
我也有一个有效的查询,但我怀疑它不会在大量数据的生产中起作用。这是
SELECT TOP 1 new_resource_value
FROM event_history_tbls
WHERE event_history_tbls.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND event_history_tbls.resource_description = 'RAM'
AND event_history_tbls.req_dt <= @todate
AND r_id = (
SELECT max(r_id)
FROM event_history_tbls t1
WHERE t1.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND t1.resource_description = 'RAM'
AND t1.req_dt <= @todate
)
GROUP BY new_resource_value
,r_id
请告诉我,如果有比上述更优化的解决方案。提前致谢。 更新
这是我的实际查询
SELECT env_name
,sys_fqdn_name
,ipv4
,(
(
CASE
WHEN (
IsNULL((
SELECT TOP 1 new_resource_value
FROM event_history_tbls
WHERE event_history_tbls.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND event_history_tbls.resource_description = 'RAM'
AND event_history_tbls.req_dt <= @todate
AND r_id = (
SELECT max(r_id)
FROM event_history_tbls t1
WHERE t1.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND t1.resource_description = 'RAM'
AND t1.req_dt <= @todate
)
GROUP BY new_resource_value
,r_id
), 0) = 0
)
THEN cast(physical_memory AS FLOAT)
ELSE cast(IsNULL((
SELECT TOP 1 new_resource_value
FROM event_history_tbls
WHERE event_history_tbls.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND event_history_tbls.resource_description = 'RAM'
AND event_history_tbls.req_dt <= @todate
AND r_id = (
SELECT max(r_id)
FROM event_history_tbls t1
WHERE t1.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND t1.resource_description = 'RAM'
AND t1.req_dt <= @todate
)
GROUP BY new_resource_value
,r_id
), 0) AS FLOAT)
END
)
) PHYSICAL_MEMORY
,no_of_hdd
,(
(
CASE
WHEN (
IsNULL((
SELECT TOP 1 new_resource_value
FROM event_history_tbls
WHERE event_history_tbls.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND event_history_tbls.resource_description = 'HDD'
AND event_history_tbls.req_dt <= @todate
AND r_id = (
SELECT max(r_id)
FROM event_history_tbls t1
WHERE t1.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND t1.resource_description = 'HDD'
AND t1.req_dt <= @todate
)
GROUP BY new_resource_value
,r_id
), 0) = 0
)
THEN cast(total_size_hdd AS FLOAT)
ELSE cast(IsNULL((
SELECT TOP 1 new_resource_value
FROM event_history_tbls
WHERE event_history_tbls.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND event_history_tbls.resource_description = 'HDD'
AND event_history_tbls.req_dt <= @todate
AND r_id = (
SELECT max(r_id)
FROM event_history_tbls t1
WHERE t1.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND t1.resource_description = 'HDD'
AND t1.req_dt <= @todate
)
GROUP BY new_resource_value
,r_id
), 0) AS FLOAT)
END
)
) TOTAL_SIZE_HDD
,(
CASE
WHEN (
IsNULL((
SELECT TOP 1 new_resource_value
FROM event_history_tbls
WHERE event_history_tbls.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND event_history_tbls.resource_description = 'CPU'
AND event_history_tbls.req_dt <= @todate
AND r_id = (
SELECT max(r_id)
FROM event_history_tbls t1
WHERE t1.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND t1.resource_description = 'CPU'
AND t1.req_dt <= @todate
)
GROUP BY new_resource_value
,r_id
), 0) = 0
)
THEN no_of_cpu
ELSE cast(IsNULL((
SELECT TOP 1 new_resource_value
FROM event_history_tbls
WHERE event_history_tbls.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND event_history_tbls.resource_description = 'CPU'
AND event_history_tbls.req_dt <= @todate
AND r_id = (
SELECT max(r_id)
FROM event_history_tbls t1
WHERE t1.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
AND t1.resource_description = 'CPU'
AND t1.req_dt <= @todate
)
GROUP BY new_resource_value
,r_id
), 0) AS FLOAT)
END
) NO_OF_CPU
,cpu_core_each
,environment
FROM sys_hdwr_dtls
WHERE UPPER(sys_hdwr_dtls.env_name) LIKE @envname
AND UPPER(sys_hdwr_dtls.sys_fqdn_name) LIKE @machinename
AND Flag = 1
答案 0 :(得分:0)
使用Row_Number
。
with cte_1
as
(SELECT *,ROW_NUMBER() OVER(PARTITION BY Machinename,Resource ORDER BY R_ID desc) RNO
FROM Yourtable)
SELECT *
FROM cte_1
WHERE Rno=1
所以你的脚本将如下所示。你需要纠正似乎错误的条件(t1.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name
)。
;WITH cte_1
AS
(SELECT *,ROW_NUMBER()OVER(PARTITION BY Machinename,Resourse ORDER BY r_id desc ) RNO
FROM event_history_tbls
WHERE event_history_tbls.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name -- Place proper condition (@gordon already pointed out this)
AND event_history_tbls.resource_description = 'RAM'
AND event_history_tbls.req_dt <= @todate)
SELECT *
FROM cte_1
WHERE RNO=1
答案 1 :(得分:0)
如果我认为你的问题是一个相关的子查询,那么最有效的代码可能就是:
SELECT TOP 1 new_resource_value
FROM event_history_tbls
WHERE event_history_tbls.sys_fqdn_name = sys_hdwr_dtls.sys_fqdn_name AND
event_history_tbls.resource_description = 'RAM' AND
event_history_tbls.req_dt <= @todate
ORDER BY r_id DESC
GROUP BY
和子查询不是必需的。
为了提高性能,您需要event_history_tbls(sys_fqdn_name, resource_description, req_dt)
上的索引。
答案 2 :(得分:0)
试试这个:
SELECT Machinename,Resource,R_Id,MemorySizeInGb
FROM TableName t1
WHERE R_Id =( SELECT MAX(R_Id)
FROM TableName t2
WHERE t1.Machinename = t2.Machinename AND t1.Resource=t2.Resource)