我试图在不使用UNION ALL语句的情况下重新创建此查询,我尝试过 标量子查询和模型子句似乎没有做我想要的。我的经理不想使用Union All,但我真的不知道替代方案是什么
任何帮助都会很棒
SELECT null AS UDA_ID,
'Style-Level Attributes' AS UDA_DESC,
null AS DISPLAY_TYPE,
null AS REQUIRED_IND,
null AS ITEM,
null AS UDA_TEXT,
null AS UDA_DATE,
null AS UDA_VALUE
FROM DUAL
UNION ALL
select a.UDA_ID,
a.UDA_DESC,
a.DISPLAY_TYPE,
a.REQUIRED_IND,
b.ITEM,
b.UDA_TEXT,
b.UDA_DATE,
b.UDA_VALUE
from DAL.P_UDA a,
FFEDEV.FFE_ITEM_UDA b
where a.UDA_ID = b.UDA_ID (+)
AND a.REQUIRED_IND IS NOT NULL
AND b.ITEM_PARENT IS NULL
UNION ALL
SELECT null AS UDA_ID,
'Additional Attributes' AS UDA_DESC,
null AS DISPLAY_TYPE,
null AS REQUIRED_IND,
null AS ITEM,
null AS UDA_TEXT,
null AS UDA_DATE,
null AS UDA_VALUE
FROM DUAL
UNION ALL
select a.UDA_ID,
a.UDA_DESC,
a.DISPLAY_TYPE,
a.REQUIRED_IND,
b.ITEM,
b.UDA_TEXT,
b.UDA_DATE,
b.UDA_VALUE
from DAL.P_UDA a,
FFEDEV.FFE_ITEM_UDA b
where a.UDA_ID = b.UDA_ID (+)
AND a.REQUIRED_IND IS NULL
AND b.ITEM_PARENT IS NULL
答案 0 :(得分:5)
我注意到在您的查询中,唯一真正的功能差异在于两个WHERE
子句。在UNION ALL
的前半部分,条件有:
WHERE a.REQUIRED_IND IS NOT NULL
而下半场有:
WHERE a.REQUIRED_IND IS NULL
您可以使用CASE
表达式生成包含属性类型的计算列。为清楚起见,这里是CASE
表达式:
CASE WHEN a.REQUIRED_IND IS NOT NULL
THEN 'Style-Level Attributes'
ELSE 'Additional Attributes'
END AS UDA_DESC
这是完整的查询:
SELECT CASE WHEN a.REQUIRED_IND IS NOT NULL THEN 'Style-Level Attributes' ELSE 'Additional Attributes' END AS AttrType,
a.UDA_ID, a.UDA_DESC, a.DISPLAY_TYPE, a.REQUIRED_IND, b.ITEM, b.UDA_TEXT, b.UDA_DATE, b.UDA_VALUE
FROM DAL.P_UDA a
LEFT OUTER JOIN FFEDEV.FFE_ITEM_UDA b
ON a.UDA_ID = b.UDA_ID
WHERE b.ITEM_PARENT IS NULL
ORDER BY AttrType
此查询中唯一的主要潜在挫折是您丢失了两个空标题行。但是,出于报告目的,应该没有问题。如果你的老板坚持那些空标题行,那么告诉他们它们是合成数据,并且将它们添加到结果集的唯一方法是执行UNION
。