2016-10-10 67 views
1

我有一個SQL查詢,如果發現文件將比較來自web_pub_subfile表file_names並顯示信息或沒有多重屬性

SELECT file_name, CASE WHEN EXISTS(SELECT 1 
      FROM web_pub_subfile t JOIN infocard_1 ic on ic.info_card_id=t.info_card_id 
      WHERE E.file_name = t.file_name and lower(ic.vault_name) ='SU-SPEC-RELEASE') 
      THEN 'YES' 
      ELSE 'NO' END AS File_Found 
FROM (VALUES 
('00924.dwg'), 
('00960.dwg'), 
('00973.dwg'), 
('00984.dwg'), 
('01008.dwg')) E(file_name) 

我會得到的輸出格式

 
file_name File_Found 
---------------------- 
00924.dwg NO 
00960.dwg YES 
00973.dwg YES 
00984.dwg YES 
01008.dwg YES 

我可以使用ABOVE查詢從TABLE - > web_pub_subfile顯示2個更多屬性(document_num和vault_name)嗎?

 
file_name File_Found document_num vault 
----------------------------------------------------- 
00924.dwg NO   Test1   SU-SPEC-RELEASE 
00960.dwg YES   Test2   SU-SPEC-RELEASE 
00973.dwg YES   Test3   SU-SPEC-RELEASE 
00984.dwg YES   Test4   SU-SPEC-RELEASE 
01008.dwg YES   Test5   SU-SPEC-RELEASE 
+1

提供web_pub_subfile和infocard_1表DEF。 – Esty

+0

即使沒有找到文件,你如何在第一行得到'Test1','SU-SPEC-RELEASE' –

+0

@Prdp第二個輸出就是例子。我試圖從web_pub_subfile獲取該信息TABLE –

回答

0

CASE語句不能返回多列

可以移動sub-queryleft join

SELECT file_name, 
     CASE 
     WHEN t.file_name IS NOT NULL THEN 'YES' 
     ELSE 'NO' 
     END AS File_Found, 
     document_num, -- Replace it with the original column name 
     vault_name -- Replace it with the original column name 
FROM (VALUES ('00924.dwg'), 
       ('00960.dwg'), 
       ('00973.dwg'), 
       ('00984.dwg'), 
       ('01008.dwg')) E(file_name) 
     LEFT JOIN web_pub_subfile t 
       ON E.file_name = t.file_name 
     LEFT JOIN infocard_1 ic 
       ON ic.info_card_id = t.info_card_id 
       AND Lower(ic.vault_name) = 'SU-SPEC-RELEASE' 
+0

感謝兄弟。它的工作,但不得不改變過去的說法 LEFT JOIN infocard_1 IC LEFT JOIN web_pub_subfile牛逼 ON ic.info_card_id = t.info_card_id AND(下(ic.vault_name)= 'SU-SPEC-RELEASE') ON E.file_name = t.file_name –