我坚持使用存在和不存在关键字。
我有一个表(Oracle Portal 表),总结如下结构:
ID LANGUAGE CAPTION
1 NL DUTCH CAPTION
1 F FRENCH CAPTION
2 NL DUTCH CAPTION
3 F FRENCH CAPTION
我需要一个选择,它总是为每个 ID 选择两行,即使 F 行或 NL 行不存在。然后应该使用列语言中不存在的语言选择该行,但使用现有行的标题(使用语言 F)
所以查询的输出应该是:
ID LANGUAGE CAPTION
1 NL DUTCH CAPTION
1 F FRENCH CAPTION
2 NL DUTCH CAPTION
2 F DUTCH CAPTION
3 F FRENCH CAPTION
3 NL FRENCH CAPTION
这可能吗?
很好奇,已经非常感谢了!!
ps:我有这样的东西,但这完全不是我想要的。
SELECT id, language, display_name
FROM portal.wwsbr_all_folders p
WHERE caid = 55
AND DISPLAY_IN_PARENT_FOLDER = 1
AND ((upper(language) = 'NL' AND not exists (SELECT * FROM portal.wwsbr_all_folders p2 WHERE p.id = p2.id and language IN ('F')))
OR
(upper(language) = 'F' AND not exists (SELECT * FROM portal.wwsbr_all_folders p2 WHERE p.id = p2.id and language IN ('NL')))
)
AND parent_id = 4030963
ORDER BY sub_folder_sequence
编辑:我现在通过以下查询得到正确的结果
(SELECT id, CASE language WHEN 'nl' THEN 'f' ELSE 'f' END lang, display_name
FROM portal.wwsbr_all_folders p
WHERE caid = 55
AND DISPLAY_IN_PARENT_FOLDER = 1
AND ((language = 'f' and exists(SELECT * FROM portal.wwsbr_all_folders p2 WHERE p2.id = p.id AND language = 'f'))
OR (language = 'nl' and not exists(SELECT * FROM portal.wwsbr_all_folders p2 WHERE p2.id = p.id AND language = 'f')))
AND parent_id = 4656102)
UNION ALL
(
SELECT id, CASE language WHEN 'f' THEN 'nl' ELSE 'nl' END lang, display_name
FROM portal.wwsbr_all_folders p
WHERE caid = 55
AND DISPLAY_IN_PARENT_FOLDER = 1
AND ((language = 'nl' and exists(SELECT * FROM portal.wwsbr_all_folders p2 WHERE p2.id = p.id AND language = 'nl'))
OR (language = 'f' and not exists(SELECT * FROM portal.wwsbr_all_folders p2 WHERE p2.id = p.id AND language = 'nl')))
AND parent_id = 4656102)