我正在使用Oracle数据库和Oracle SQL Developer 4
我正在寻找一种方法来查明我是否已经在多个表上使用了ID,而这些表是我加入了一个联合的。这应该是名为HAS_BEEN_USED
的列,如果在其中一个表中使用过一次或多次,则值为“是”,如果未在任何表中使用,则为“否”。
如果ID在任何表中出现一次:HAS_BEEN_USED = YES
如果不是:HAS_BEEN_USED = NO
这样的事情:
SELECT PRICETITLEID, IF(PRICETITLEID>0, 'Yes', 'No') AS HAS_BEEN_USED FROM ZZPRICETITLE
UNION
SELECT PRICETITLEID, IF(COUNT(PRICETITLEID)>0, 'Yes', 'No') AS HAS_BEEN_USED FROM ZZEXTVALUATIONRULE
UNION
SELECT PRICETITLEID, IF(COUNT(PRICETITLEID)>0, 'Yes', 'No') AS HAS_BEEN_USED FROM ZZMARKET
UNION
SELECT PRICETITLEID, IF(COUNT(PRICETITLEID)>0, 'Yes', 'No') AS HAS_BEEN_USED FROM ZZMARKETUSE
UNION
SELECT PRICETITLEID, IF(COUNT(PRICETITLEID)>0, 'Yes', 'No') AS HAS_BEEN_USED FROM ZZPRICE
WHERE TIMECREATED >= '01-FEB-17'
GROUP BY PRICETITLEID;
评论任何问题
由于
答案 0 :(得分:0)
试试这个
SELECT PRICETITLEID, CASE WHEN PRICETITLEID>0 THEN "Yes" ELSE "No" END AS HAS_BEEN_USED FROM ZZPRICETITLE
UNION
SELECT PRICETITLEID, CASE WHEN COUNT(PRICETITLEID)>0 THEN "Yes" ELSE "No" END AS HAS_BEEN_USED FROM ZZEXTVALUATIONRULE
UNION
SELECT PRICETITLEID, CASE WHEN COUNT(PRICETITLEID)>0 THEN 'Yes' ELSE 'No' END AS HAS_BEEN_USED FROM ZZMARKET
UNION
SELECT PRICETITLEID, CASE WHEN COUNT(PRICETITLEID)>0 THEN 'Yes' ELSE 'No' END AS HAS_BEEN_USED FROM ZZMARKETUSE
UNION
SELECT PRICETITLEID, CASE WHEN COUNT(PRICETITLEID)>0 THEN 'Yes' ELSE 'No' END AS HAS_BEEN_USED FROM ZZPRICE
WHERE TIMECREATED >= '01-FEB-17'
GROUP BY PRICETITLEID;