我被困住了,我需要一个建议如何将2个结果集合并为1
第一个结果集具有用于不同安装点的阈值。 每个安装点都有一个带有非默认阈值的行,例如“ u01”-“ u08”。所有其他安装点的默认阈值位于带有空MOUNTPOINT值的行中。
HOSTNAME MOUNTPOINT WARNING_PC CRITICAL_P
------------------------------ ---------------------- ---------- ----------
oracledatabase.domain.local 20 5 <-- here is default
oracledatabase.domain.local /opt/nfsshare/u01 20 19
oracledatabase.domain.local /opt/nfsshare/u02 1.01 1
oracledatabase.domain.local /opt/nfsshare/u03 1.01 1
oracledatabase.domain.local /opt/nfsshare/u04 1.01 1
oracledatabase.domain.local /opt/nfsshare/u05 1.01 1
oracledatabase.domain.local /opt/nfsshare/u07 1.01 1
oracledatabase.domain.local /opt/nfsshare/u08 1.01 1
第二个结果集包含所有安装点的列表
HOSTNAME MOUNTPOINT
------------------------------ ----------------------------------------
oracledatabase.domain.local /
oracledatabase.domain.local /boot
oracledatabase.domain.local /dev
oracledatabase.domain.local /opt/nfsshare/u01
oracledatabase.domain.local /opt/nfsshare/u02
oracledatabase.domain.local /opt/nfsshare/u03
oracledatabase.domain.local /opt/nfsshare/u04
oracledatabase.domain.local /opt/nfsshare/u05
oracledatabase.domain.local /opt/nfsshare/u06
oracledatabase.domain.local /opt/nfsshare/u07
oracledatabase.domain.local /opt/nfsshare/u08
oracledatabase.domain.local /opt/nfsshare/u09
oracledatabase.domain.local /opt/nfsshare/u10
oracledatabase.domain.local /opt/nfsshare/u11
oracledatabase.domain.local /opt/nfsshare/u12
oracledatabase.domain.local /opt/nfsshare/u13
因此,我需要通过以下方式加入他们: 如果安装点在第一个结果集中具有阈值,则使用该值。 如果没有安装点的阈值,则使用默认值
HOSTNAME MOUNTPOINT WARNING_PC CRITICAL_P
------------------------------ ------------------- ---------- ----------
oracledatabase.domain.local / 20 5
oracledatabase.domain.local /boot 20 5
oracledatabase.domain.local /dev 20 5
oracledatabase.domain.local /opt/nfsshare/u01 20 19
oracledatabase.domain.local /opt/nfsshare/u02 1.01 1
oracledatabase.domain.local /opt/nfsshare/u03 1.01 1
oracledatabase.domain.local /opt/nfsshare/u04 1.01 1
oracledatabase.domain.local /opt/nfsshare/u05 1.01 1
oracledatabase.domain.local /opt/nfsshare/u06 20 5
oracledatabase.domain.local /opt/nfsshare/u07 1.01 1
oracledatabase.domain.local /opt/nfsshare/u08 1.01 1
oracledatabase.domain.local /opt/nfsshare/u09 20 5
oracledatabase.domain.local /opt/nfsshare/u10 20 5
oracledatabase.domain.local /opt/nfsshare/u11 20 5
oracledatabase.domain.local /opt/nfsshare/u12 20 5
oracledatabase.domain.local /opt/nfsshare/u13 20 5
几个小时后添加。 看来我已经找到了解决方案。但是可能有更好的方法
COL HOSTNAME2 FOR A30
COL MOUNTPOINT2 FOR A30
WITH THRESHOLDS AS (
SELECT TARGET_NAME "HOSTNAME1",
DECODE(LENGTH(REPLACE(KEY_VALUE,CHR(32))),NULL,'DEFAULT',KEY_VALUE) "MOUNTPOINT1",
WARNING_THRESHOLD "WARNING_PCT",
CRITICAL_THRESHOLD "CRITICAL_PCT"
FROM
SYSMAN.MGMT$METRIC_COLLECTION
WHERE
TARGET_TYPE = 'host' AND
METRIC_NAME = 'Filesystems' AND
METRIC_COLUMN = 'pctAvailable' AND TARGET_NAME='oracledatabase.domain.local'
),
MOUNTPOINTS AS (
SELECT ENTITY_NAME "HOSTNAME2",
KEY_PART_1 "MOUNTPOINT2"
FROM
SYSMAN.GC$METRIC_VALUES_LATEST
WHERE
METRIC_GROUP_LABEL = 'Filesystems' AND METRIC_COLUMN_NAME = 'available'
AND ENTITY_NAME='oracledatabase.domain.local'
)
-- Get list of matching mountpoints
SELECT HOSTNAME2, MOUNTPOINT2, WARNING_PCT, CRITICAL_PCT FROM (
SELECT * FROM MOUNTPOINTS INNER JOIN THRESHOLDS ON
MOUNTPOINTS.MOUNTPOINT2=THRESHOLDS.MOUNTPOINT1)
-- Union
UNION ALL
-- Get list mountpoints with default threshold
SELECT HOSTNAME2, MOUNTPOINT2, WARNING_PCT, CRITICAL_PCT FROM
(SELECT * FROM MOUNTPOINTS WHERE MOUNTPOINT2 NOT IN (SELECT MOUNTPOINT1 FROM THRESHOLDS)),
(SELECT * FROM THRESHOLDS WHERE MOUNTPOINT1='DEFAULT')
ORDER BY 2;
HOSTNAME2 MOUNTPOINT2 WARNING_PC CRITICAL_PCT
------------------------------ ------------------------------ ---------- --------------------
oracledatabase.domain.local / 20 5
oracledatabase.domain.local /boot 20 5
oracledatabase.domain.local /dev 20 5
oracledatabase.domain.local /opt/nfsshare/u01 20 19
oracledatabase.domain.local /opt/nfsshare/u02 1.01 1
oracledatabase.domain.local /opt/nfsshare/u03 1.01 1
oracledatabase.domain.local /opt/nfsshare/u04 1.01 1
oracledatabase.domain.local /opt/nfsshare/u05 1.01 1
oracledatabase.domain.local /opt/nfsshare/u06 20 5
oracledatabase.domain.local /opt/nfsshare/u07 1.01 1
oracledatabase.domain.local /opt/nfsshare/u08 1.01 1
oracledatabase.domain.local /opt/nfsshare/u09 20 5
oracledatabase.domain.local /opt/nfsshare/u10 20 5
oracledatabase.domain.local /opt/nfsshare/u11 20 5
oracledatabase.domain.local /opt/nfsshare/u12 20 5
oracledatabase.domain.local /opt/nfsshare/u13 20 5
16 rows selected.
有关“ rs”的详细信息。评论(仅供我参考)
没有COALESCE功能,更易于理解其工作原理。
WITH THRESHOLDS AS (
SELECT TARGET_NAME "HOSTNAME1",
KEY_VALUE "MOUNTPOINT1",
WARNING_THRESHOLD "WARNING_PCT",
CRITICAL_THRESHOLD "CRITICAL_PCT"
FROM
SYSMAN.MGMT$METRIC_COLLECTION
WHERE
TARGET_TYPE = 'host' AND
METRIC_NAME = 'Filesystems' AND
METRIC_COLUMN = 'pctAvailable' AND TARGET_NAME='oracledatabase.domain.local'
),
MOUNTPOINTS AS (
SELECT ENTITY_NAME "HOSTNAME2",
KEY_PART_1 "MOUNTPOINT2"
FROM
SYSMAN.GC$METRIC_VALUES_LATEST
WHERE
METRIC_GROUP_LABEL = 'Filesystems' AND METRIC_COLUMN_NAME = 'available'
AND ENTITY_NAME='oracledatabase.domain.local'
)
SELECT A.HOSTNAME2, A.MOUNTPOINT2,
T1.WARNING_PCT "WRN_THR",
T2.WARNING_PCT "WRN_DFL",
T1.CRITICAL_PCT "CRT_THR",
T2.CRITICAL_PCT "CRT_DFL"
FROM MOUNTPOINTS A
LEFT OUTER JOIN THRESHOLDS T1 ON A.MOUNTPOINT2 = T1.MOUNTPOINT1 --match
JOIN THRESHOLDS T2 ON COALESCE(TRIM(T2.MOUNTPOINT1),'DEFAULT') = 'DEFAULT';
HOSTNAME2 MOUNTPOINT2 WRN_THR WRN_DFL CRT_THR CRT_DFL
------------------------------ -------------------- ------- ------- ------- -------
oracledatabase.domain.local /opt/nfsshare/u01 20 20 19 5
oracledatabase.domain.local /opt/nfsshare/u02 1.01 20 1 5
oracledatabase.domain.local /opt/nfsshare/u03 1.01 20 1 5
oracledatabase.domain.local /opt/nfsshare/u04 1.01 20 1 5
oracledatabase.domain.local /opt/nfsshare/u05 1.01 20 1 5
oracledatabase.domain.local /opt/nfsshare/u07 1.01 20 1 5
oracledatabase.domain.local /opt/nfsshare/u08 1.01 20 1 5
oracledatabase.domain.local /boot 20 5
oracledatabase.domain.local /opt/nfsshare/u06 20 5
oracledatabase.domain.local /opt/nfsshare/u13 20 5
oracledatabase.domain.local /dev 20 5
oracledatabase.domain.local /opt/nfsshare/u11 20 5
oracledatabase.domain.local /opt/nfsshare/u12 20 5
oracledatabase.domain.local / 20 5
oracledatabase.domain.local /opt/nfsshare/u10 20 5
oracledatabase.domain.local /opt/nfsshare/u09 20 5
16 rows selected.
https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions023.htm#SQLRF00617 COALESCE 返回表达式列表中的第一个非空expr 。您必须至少指定两个表达式。 如果所有出现的expr都评估为null,则该函数返回null。
答案 0 :(得分:0)
您可以使用原始表,联接和没有CTE来实现此目的
例如,假设您的表是MOUNTPOINTS和THRESHOLDS:
SELECT A.HOSTNAME, A.MOUNTPOINT,
COALESCE(T1.WARNING_PCT,T2.WARNING_PCT) WARNING_PCT,
COALESCE(T1.CRITICAL_PCT, T2.CRITICAL_PCT) CRITICAL_PCT
FROM MOUNTPOINTS A
LEFT OUTER JOIN THRESHOLDS T1 ON A.MOUNTPOINT = T1.MOUNTPOINT --match
JOIN THRESHOLDS T2 ON COALESCE(TRIM(T2.MOUNTPOINT),'DEFAULT') = 'DEFAULT'
请注意,如果它具有space('')为空字符串(在oracle中被视为NULL),则在此处使用TRIM更改默认值。