条件a if b条件的Oracle SQL选择不存在

时间:2013-10-15 08:37:57

标签: sql oracle exists not-exists

我坚持使用exists和not exists关键字。

我有一个表(Oracle Portal表),总结了以下结构:

ID    LANGUAGE    CAPTION
1     NL          DUTCH CAPTION
1     F           FRENCH CAPTION
2     NL          DUTCH CAPTION
3     F           FRENCH CAPTION

我需要一个select,它总是为每个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)

2 个答案:

答案 0 :(得分:1)

你只有两种语言吗?如果是这样,你可以尝试以下解决方案:

WITH
  my_data AS
    (           SELECT 1 AS id, 'NL' AS language, 'DUTCH CAPTION' AS caption FROM dual
      UNION ALL SELECT 1 AS id, 'F' AS language, 'FRENCH CAPTION' AS caption FROM dual
      UNION ALL SELECT 2 AS id, 'NL' AS language, 'DUTCH CAPTION' AS caption FROM dual
      UNION ALL SELECT 3 AS id, 'F' AS language, 'FRENCH CAPTION' AS caption FROM dual)
SELECT
      id, language, caption
  FROM my_data md
UNION ALL
SELECT
      id, DECODE(language, 'NL', 'F', 'NL'), caption
  FROM my_data md
WHERE NOT EXISTS (SELECT 1
                    FROM my_data
                  WHERE id = md.id
                    AND language != md.language)
ORDER BY id, language;

答案 1 :(得分:0)

类似以下内容。

如果您有一个包含ID的额外表,则可以使用它代替id_list并省略WITH部分。

WITH id_list AS 
( SELECT DISTINCT id FROM portal_table)
SELECT *
FROM 
(
    -- FRENCH
    SELECT 
      i.id  as id
    , 'F'   as language 
    , NVL(f.CAPTION, NL.CAPTION) AS CAPTION
    FROM 
        id_list  i 
      LEFT OUTER JOIN portal_table f   ON (f.id = i.id  AND f.language = 'F') 
      LEFT OUTER JOIN portal_table nl  ON (nl.id = i.id AND nl.language = 'NL') 
  UNION
      -- DUTCH 
    SELECT 
      i.id  as id
    , 'NL'  as language 
    , NVL(NL.CAPTION, F.CAPTION) AS CAPTION
    FROM 
        id_list  i 
      LEFT OUTER JOIN portal_table f   ON (f.id = i.id  AND f.language = 'F') 
      LEFT OUTER JOIN portal_table nl  ON (nl.id = i.id AND nl.language = 'NL') 
)
order by id
;