Question

I'm stuck with the exists and not exists keywords.

I have a table (Oracle Portal table) which has, summarized, following structure:

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

I need a select which always selects two rows per ID, even if a F row or NL row does not exists. Then the row should be selected with the non-existing language in column language but with the caption of the existing row (with language F)

So the output of the query should be :

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

Is this possible?

Very curious, and already a big big thanks!!

ps: I have something like this but it's totally not what I want.

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

EDIT: I'm getting the right results with the following query now

(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)
Was it helpful?

Solution

Do you have only two languages? If so, you could try below solution:

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;

OTHER TIPS

somthing like the following.

if you have an extra table with IDs you can use it istead of id_list and omit the WITH part.

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
;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top