0

我坚持使用存在和不存在关键字。

我有一个表(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)
4

2 回答 2

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;
于 2013-10-15T08:59:52.750 回答
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
;
于 2013-10-15T08:56:24.210 回答