我正在尝试使用xmlagg将行转换为逗号分隔列表。我遇到的麻烦是col2中的重复值。我需要为col2中的每个重复值添加一个新行。
这是源数据:
+-----------------+-----------------+------------+
| Col1 | Col2 | Col3 |
+-----------------+-----------------+------------+
| VSPARK 27002026 | account_id | account_id |
| VSPARK 27002026 | account_name | Kontod 6-7 |
| VSPARK 27002026 | account_name | Kontod 1-3 |
| VSPARK 27002026 | account_name | Kontod 4-6 |
| VSPARK 27002026 | cash_type | CASH_TYP |
| VSPARK 27002026 | Currency | CURRENCY |
| VSPARK 27002026 | cust_type | CUST_TYP |
| VSPARK 27002026 | Residency | RESIDENCY |
| VSPARK 27002026 | transaction_amt | AMT |
+-----------------+-----------------+------------+
这是预期的输出:
+-----------------+--------------------------------------------------------------------------------------+-----------------------------------------------------------------------+
| Col1 | Col2 | Col3 |
+-----------------+--------------------------------------------------------------------------------------+-----------------------------------------------------------------------+
| VSPARK 27002026 | account_name, account_id, cash_type, cust_type, Residency, transaction_amt, Currency | Kontod 6-7, account_id, CASH_TYP, CUST_TYP, RESIDENCY, AMT, CURRENCY |
| VSPARK 27002026 | account_name, account_id, cash_type, cust_type, Residency, transaction_amt, Currency | Kontod 1-3, account_id, CASH_TYP, CUST_TYP, RESIDENCY, AMT, CURRENCY |
| VSPARK 27002026 | account_name, account_id, cash_type, cust_type, Residency, transaction_amt, Currency | Kontod 4-6, account_id, CASH_TYP, CUST_TYP, RESIDENCY, AMT, CURRENCY |
+-----------------+--------------------------------------------------------------------------------------+-----------------------------------------------------------------------+
col2中的每个重复值都应在最终结果中创建一个额外的行。不幸的是我不知道如何做到这一点。
欢迎任何帮助!
EDIT1:
这是我目前的SQL:
SELECT report_name,
TRIM(TRAILING ','
FROM (XMLAGG(TRIM(insert_into_fields)|| ','
ORDER BY report_name) (VARCHAR(10000)))),
TRIM(TRAILING ','
FROM (XMLAGG(TRIM(select_from_fields)|| ','
ORDER BY report_name) (VARCHAR(10000))))
FROM
(SELECT report_name,
field_name insert_into_fields,
CASE
WHEN account_mapping_formula_id IS NOT NULL THEN mapping_name
WHEN formula IS NULL
AND account_mapping_Formula_id IS NULL THEN source_field_name
ELSE formula
END select_from_fields
FROM mapping rf
LEFT JOIN report r ON rf.report_id = r.report_id
LEFT JOIN report_field e ON rf.target_column_id = e.field_id
LEFT JOIN source_fields c ON rf.source_field_id = c.source_field_id
LEFT JOIN account_mapping d ON rf.account_mapping_formula_id = d.account_mapping_id
WHERE rf.report_id = 2) asd
GROUP BY report_name;
这是当前的输出:
+-----------------+-----------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------+
| Col1 | Col2 | Col3 |
+-----------------+-----------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------+
| VSPARK 27002026 | account_name, account_name, account_name, account_id, cash_type,cust_type, Residency, transaction_amt, Currency | Kontod 6-7, Kontod 1-3, Kontod 4-6, account_id, CASH_TYP, CUST_TYP, RESIDENCY, AMT, CURRENCY |
+-----------------+-----------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------+
答案 0 :(得分:0)
哎呀,这不是一个用户友好的数据模型:)
您需要复制每个account_name的其他行。一种解决方案是加入(Col1,Col3)
的不同列表,然后将Col3
添加到最终GROUP BY
,类似这样的
WITH cte AS
(
SELECT *
FROM asd -- this is your current Derived Table
)
SELECT t2.report_name,
'account_name' || ','||
Trim(Trailing ','
FROM (XmlAgg(Trim(t1.insert_into_fields)|| ','
-- Either sort by a unique column or no sort at all
ORDER BY t1.insert_into_fields) (VARCHAR(10000)))),
t2.select_from_fields || ','||
Trim(Trailing ','
FROM (XmlAgg(Trim(t1.col3)|| ','
-- Either sort by a unique column or no sort at all
ORDER BY t1.insert_into_fields) (VARCHAR(10000))))
FROM cte AS t1
JOIN
( -- list of report/account combinations
SELECT DISTINCT report_name, select_from_fields
FROM cte
WHERE insert_into_fields = 'account_name'
) AS t2
ON t1.report_name = t2.report_name
WHERE t1.insert_into_fields <> 'account_name'
GROUP BY t2.report_name, t2.select_from_fields