2016-10-27 19 views
0

xmlaggを使用して、行を昏睡区切りのリストに変換しようとしています。私が問題を抱えているのはcol2の繰り返し値です。 col2のすべての繰り返し値に対して新しい行が必要です。テラデータの繰り返し行を1つにマージする

これは、ソースデータである:

+-----------------+-----------------+------------+ 
|  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

のようなものは、あなたがヨール現在のSQLを表示することができ、(Col1,Col3)の明確なリストに参加し、その後、最終的なGROUP BYCol3を追加することですか? – dnoeth

+0

確かに、私は使用しているSQLを使って元の投稿を編集します。私はそれと一緒に得るcurrect出力を投稿します。 – andrepaal

答えて

0

痛い、それはユーザーフレンドリーなデータモデルではありません:)

ea用に他の行を複製する必要がありますchアカウント名。一つの解決策は、この

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 
関連する問題