2012-11-05 366 views
1

我在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 

    ; 
+0

看起來像一個絕對可怕的架構。對於「永遠運行」的查詢 - 「解釋」報告是什麼? '解釋選擇...' –

回答

1

是否有可能爲你使用OTRS API,而不是查詢?以這種方式獲取數據會更容易一些。 此外,ImportExport模塊允許將數據導出到.csv文件;根據您的需要,這可能是一個選擇。