Oracle
 sql >> Base de données >  >> RDS >> Oracle

Si les données existent, sinon... utiliser Oracle SQL ?

QUERY1
UNION ALL
QUERY2
WHERE NOT EXISTS (QUERY1)

ex :

SELECT id
  FROM products
    UNION ALL 
SELECT  id
  FROM old_products
    WHERE NOT EXISTS (SELECT id  FROM products) 

Requête :

SELECT * 
    FROM Table 
WHERE ID = 7 
    AND Flag = 1
UNION ALL
SELECT * 
    FROM Table 
WHERE ID = 7 
    AND NOT EXISTS (select * from Table where ID = 7 and Flag = 1)

Pour identifier des lignes uniques, vous pouvez également essayer ceci :

WITH qry AS
(
    SELECT a.* ,
        ROW_NUMBER() OVER(PARTITION BY id ORDER BY  Flag DESC ) rn
        FROM Table a
    WHERE ID = 7  -- THIS CAN BE PARAMETERIZED
)
SELECT * 
    FROM qry
WHERE rn = 1