2017-05-16 20 views
1

我有查詢,讓我documentid申請加入兩個結果集 - Postgres的

select documentid from tbldocumentbyclient 
where tbldocumentbyclient.isactive = true 
and applicationid = '000116' 

結果: enter image description here

另一個查詢給我下面的結果

SELECT documentcategory,documentcategoryid, string_agg(documentid::text, ',') as documentid 
         FROM tbldocumentmaster 
         where accounttype = 1 and usertype= 'INDIVIDUAL' 
         group by documentcategory,documentcategoryid 
         order by documentcategoryid; 

結果: enter image description here

現在,可有人請建議我怎麼去不與任何documentid

對於以上情況,我的結果應該是繼相關類別名稱。

  1. DP證明 - 134在第一個結果不可用
  2. 住址證明永久 - 排4 ..不是一個單一的ID提供documenid導致

這裏這是不與關聯的documentcategory任何文件ID

+0

用'pplicationid =「000116''任何文件.. –

+0

請仔細閱讀http://meta.stackoverflow.com/questions/285551/why-may-i-not-upload-images-of-code-on-so-when-asking-a-question/285557和接受的答案 –

+0

@a_horse_with_no_name ..跟隨的建議.. thks –

回答

2

我覺得你的兩個表應該做的伎倆之間的簡單連接。這裏的邏輯是,如果從tbldocumentmaster文檔類別真的有符合其中任何在tbldocumentbyclient表則連接應該將其過濾掉,留下那些做比賽類別背後沒有文檔ID。

SELECT t1.documentcategory 
FROM tbldocumentmaster t1 
LEFT JOIN tbldocumentbyclient t2 
    ON t1.documentid = t2.documentid 
WHERE t2.isactive = true   AND 
     t2.applicationid = '000116' AND 
     t1.accounttype = 1   AND 
     t1.usertype = 'INDIVIDUAL' AND 
     t2.documentid IS NULL 
+0

Th anks @Tim,它給了我與documentid相關的總共6個類別名稱。我想只提取那些在我的第一個查詢結果中不可用的那兩個categoryName。 –

+0

@MannanBahelim我更新了我的答案。 –

1

試試這個:

SELECT distinct documentcategory,documentcategoryid 
FROM tbldocumentmaster m 
LEFT OUTER JOIN tbldocumentbyclient d on d.documentid = m.documentid 
where accounttype = 1 and usertype= 'INDIVIDUAL' 
and m.documentid is null 
and applicationid = '000116' 
group by documentcategory,documentcategoryid 
order by documentcategoryid; 
+0

感謝您的回覆,但它顯示空白的結果。我想只提取那些在我的第一個查詢結果中不可用的那兩個categoryName。 –

0

我沒有測試過,但你可以嘗試此查詢:?

SELECT documentcategory,documentcategoryid, string_agg(documentid::text, ',') as documentid 
         FROM tbldocumentmaster 
         where accounttype = 1 and usertype= 'INDIVIDUAL' and documentid not in (select documentid from tbldocumentbyclient where tbldocumentbyclient.isactive = true and applicationid = '000116') 
         group by documentcategory,documentcategoryid 
         order by documentcategoryid;