我在从 OTRS 对 postgresql 数据库创建查询时遇到问题。我需要获取一个资产列表,其中包含一些存储在 CMDB 中的数据,但是我在存储数据的方式上遇到了问题。
就目前而言,此查询可以正常工作,但是一旦我添加另一个左联接以获取更多数据,它就会永远运行而不会返回任何结果。
据我所见,问题在于所有信息都包含在 xml_storage 表中,没有任何关系,因此我需要重复查询该表以获取数据。还有多种数据类型,其中整数在某些情况下存储为可变字符,这使我无法使用该值从通用目录中获取其他信息。
几天来我一直在用头撞砖墙,所以我真的很感激任何帮助
SELECT
asset_value,
manufacturer_value,
model_value,
serial_value,
fqdn_value,
array_agg(service),
purchasedate_value,
status_value,
configitem.configitem_number,
configitem_version.name,
general_catalog.name
FROM
public.configitem_version,
public.general_catalog,
public.configitem
left join
(SELECT
xml_storage.xml_content_value AS asset_value,
xml_storage.xml_key
FROM
public.xml_storage
WHERE
xml_content_key = '[1]{''Version''}[1]{''Asset Number''}[1]{''Content''}') as select_asset on last_version_id::int = select_asset.xml_key::int
left join
(SELECT
xml_storage.xml_content_value AS manufacturer_value,
xml_storage.xml_key
FROM
public.xml_storage
WHERE
xml_content_key = '[1]{''Version''}[1]{''Manufacturer''}[1]{''Content''}' ) as select_manufacturer on last_version_id::int = select_manufacturer.xml_key::int
left join
(SELECT
xml_storage.xml_content_value AS model_value,
xml_storage.xml_key
FROM
public.xml_storage
WHERE
xml_content_key = '[1]{''Version''}[1]{''Model''}[1]{''Content''}' ) as select_model on last_version_id::int = select_model.xml_key::int
left join
(SELECT
xml_storage.xml_content_value AS serial_value,
xml_storage.xml_key
FROM
public.xml_storage
WHERE
xml_content_key = '[1]{''Version''}[1]{''SerialNumber''}[1]{''Content''}' ) as select_serial on last_version_id::int = select_serial.xml_key::int
left join
(SELECT
xml_storage.xml_content_value AS fqdn_value,
xml_storage.xml_key
FROM
public.xml_storage
WHERE
xml_content_key = '[1]{''Version''}[1]{''FQDN''}[1]{''Content''}' ) as select_fqdn on last_version_id::int = select_fqdn.xml_key::int
left join
(SELECT
link_relation.source_key AS "configitem_id",
service.name AS "service"
FROM
public.link_relation,
public.service
WHERE
link_relation.target_key::int = service.id AND
link_relation.target_object_id::int = 2 AND link_relation.source_object_id::int = 3
UNION
SELECT
link_relation.target_key AS "configitem_id",
service.name AS "service"
FROM
public.link_relation,
public.service
WHERE
link_relation.source_key::int = service.id AND
link_relation.target_object_id::int = 3 AND link_relation.source_object_id::int = 2) as select_service on configitem.id = configitem_id::int
left join
(SELECT
xml_storage.xml_content_value AS purchasedate_value,
xml_storage.xml_key
FROM
public.xml_storage
WHERE
xml_content_key = '[1]{''Version''}[1]{''PurchaseDate''}[1]{''Content''}' ) as select_purchasedate on last_version_id::int = select_purchasedate.xml_key::int
left join
(SELECT
general_catalog.name AS status_value,
general_catalog.id
FROM
public.general_catalog) as select_status on configitem.cur_depl_state_id::int = select_status.id::int
WHERE
configitem.last_version_id = configitem_version.id AND
configitem.class_id = general_catalog.id AND
(configitem.class_id = 32 OR configitem.class_id = 33)
GROUP BY
asset_value,
manufacturer_value,
model_value,
serial_value,
fqdn_value,
purchasedate_value,
status_value,
configitem.configitem_number,
configitem_version.name,
general_catalog.name
;