我有以下SELECT:
(SELECT DISTINCT
V1.FORPDM_ID AS HVID,
1 AS IMPORT_STATUS,
V1.CREATEDAT AS IMPORT_DATE,
'ok' AS IMPORT_MESSAGE,
V1.ID AS AT_ID,
V1.FORPDM_TRANSACTION_ID AS TRANSACTION_ID
FROM V_VALIDATIONCARRIER V1, V_FORPDM_DAVE_HWDEMAND V2
WHERE
V1.FORPDM_ID = V2.HVID AND -- JOIN CONDITION
...other conditions...
)
UNION ALL
(SELECT DISTINCT
V1.FORPDM_ID AS HVID,
0 AS IMPORT_STATUS,
V1.CREATEDAT AS IMPORT_DATE,
'PC_ID is null or not in V_PLANNINGCATEGORY' AS IMPORT_MESSAGE,
V1.ID AS AT_ID,
V1.FORPDM_TRANSACTION_ID AS TRANSACTION_ID
FROM V_VALIDATIONCARRIER V1, V_FORPDM_DAVE_HWDEMAND V2
WHERE (V1.FORPDM_ID = V2.HVID) AND -- JOIN CONDITION
...some error conditions...
)
UNION ALL
(SELECT DISTINCT
V1.FORPDM_ID AS HVID,
0 AS IMPORT_STATUS,
V1.CREATEDAT AS IMPORT_DATE,
'BAUPHASE is null or not in V_SPEEDSPECIFICMASTERDATA' AS IMPORT_MESSAGE,
V1.ID AS AT_ID,
V1.FORPDM_TRANSACTION_ID AS TRANSACTION_ID
FROM V_VALIDATIONCARRIER V1, V_FORPDM_DAVE_HWDEMAND V2
WHERE (V1.FORPDM_ID = V2.HVID) AND -- JOIN CONDITION
...some other error conditions...
)
在我的数据库中,有些行可以匹配错误条件,并且我想在发生这种情况时连接两个IMPORT_MESSAGE
,这样我就不会得到两行HVID
和两个不同的IMPORT_MESSAGE
当我在查询中使用UNION
时,有没有办法在Oracle中实现这一点?
修改
经过一些测试,它起作用了。只需澄清一点:要将我的所有数据和添加到我应该选择的消息中,如下所示:
SELECT DISTINCT HVID, IMPORT_STATUS, IMPORT_DATE, TRANSACTION_ID, AT_ID, LISTAGG(IMPORT_MESSAGE, ',')
WITHIN GROUP (ORDER BY IMPORT_MESSAGE) AS
IMPORT_MESSAGE FROM ( ...my union all select... ) MYUNION
GROUP BY HVID, IMPORT_STATUS, IMPORT_DATE, TRANSACTION_ID, AT_ID;
答案 0 :(得分:1)
我认为您必须在Oracle环境中使用LISTAGG
函数。
例如(见:http://www.sqlines.com/mysql/functions/group_concat):
SELECT LISTAGG(IMPORT_MESSAGE, ',') WITHIN GROUP (ORDER BY NULL)
FROM (
.... your union all select
) as YOURUNION
GROUP BY YOURUNION.IMPORT_MESSAGE;
我没有Oracle数据库,所以你必须尝试。