Talend Open Studio新手ETL问题:如何动态聚合行并添加动态日期
Hey there! As someone who’s built plenty of ETL jobs in Talend Open Studio, let’s break down how to tackle your specific task—aggregating rows dynamically and turning dates into dynamic columns from your SQL Server fact_Table.
First, let’s clarify your source data to make sure we’re on the same page:
| id_facture | produit | mouvment | quantité_Stock | Date |
|---|---|---|---|---|
| f1 | p1 | entrée | +50 | 28/04/2018 |
| f2 | p1 | entrée | +10 | 01/05/2018 |
| f3 | p1 | sortie | -20 | 02/05/2018 |
| f3 | p2 | entrée | +4 | 02/05/2018 |
| f4 | p2 | sortie | -1 | 03/05/2018 |
| f4 | p1 | entrée | +2 | 03/05/2018 |
It looks like you want to:
- Aggregate stock movements by product (and date, I assume)
- Pivot those dates into dynamic columns so each date becomes a column header for stock changes/cumulative stock
Let’s go through each step in detail:
Step 1: Connect to your SQL Server Source
First, set up your connection to the SQL Server database:
- Create a new Job in Talend Open Studio.
- Drag a
tMSSqlConnectioncomponent onto the canvas, then fill in your server details (host, port, database name, credentials) to establish the connection. - Add a
tMSSqlInputcomponent, link it to thetMSSqlConnection, and write a query to pull your data. Important: Clean up the data types first—yourquantité_Stockhas+signs, and dates are in dd/mm/yyyy format, so adjust the query to fix this:
This will make downstream processing way smoother.SELECT id_facture, produit, mouvment, -- Convert stock quantity to integer by removing the + sign CAST(REPLACE(quantité_Stock, '+', '') AS INT) AS quantité_Stock, -- Convert dd/mm/yyyy string to a proper DATE type CONVERT(DATE, Date, 103) AS Date FROM fact_Table
Step 2: Dynamic Row Aggregation (Group by Product + Date)
Next, we’ll aggregate the stock movements so we have one row per product per date:
- Drag a
tAggregateRowcomponent and connect it to yourtMSSqlInput. - In the
tAggregateRowconfiguration:- Under Group by, check both
produitandDate—this groups all movements for a product on a single date. - Under Operations, add a
Sumoperation forquantité_Stock, and name the output fielddaily_stock_change. This gives you the total stock change for each product each day.
- Under Group by, check both
Step 3: Turn Dates into Dynamic Columns (Pivot)
Now we’ll pivot those date rows into columns—Talend’s tPivotToColumns component is perfect for this dynamic use case:
- Drag a
tPivotToColumnscomponent and link it totAggregateRow. - Configure it like this:
- Pivot key: Select
Date—this is the field we want to turn into columns. - Group by: Select
produit—each row in the output will represent one product. - Aggregate on: Choose
daily_stock_changewith aSumaggregation (since we already grouped by date, this just ensures no duplicates slip through). - Check the Dynamic column box—this tells Talend to automatically create columns for every unique date in your source data, no hardcoding needed!
- Pivot key: Select
Optional: Calculate Cumulative Stock
If you want cumulative stock (not just daily changes) in your pivot table, add a tCumulateRow between tAggregateRow and tPivotToColumns:
- Configure
tCumulateRow:- Group by:
produit(so we calculate cumulative stock per product). - Sort by:
Date(critical—we need to process dates in order to get accurate cumulative totals). - Cumulate on:
daily_stock_change, name the outputcumulative_stock.
- Group by:
- Then use
cumulative_stockas the aggregate field intPivotToColumnsinstead ofdaily_stock_change.
Step 4: Output to Your Target Table
Finally, write the transformed data to your target SQL Server table:
- Drag a
tMSSqlOutputcomponent, link it totPivotToColumns. - Configure it to use your existing SQL Server connection, enter your target table name (e.g.,
product_stock_summary). - Check Create table if not exists if the table doesn’t exist yet—Talend will auto-create the table with the dynamic date columns.
- Map the fields (Talend will auto-match most fields; just double-check the dynamic date columns are correctly mapped).
Test the Job
Hit the run button, check the logs for errors, then query your target table. You should end up with something like this (for daily changes):
| produit | 2018-04-28 | 2018-05-01 | 2018-05-02 | 2018-05-03 |
|---|---|---|---|---|
| p1 | 50 | 10 | -20 | 2 |
| p2 | NULL | NULL | 4 | -1 |
Or this if you went with cumulative stock:
| produit | 2018-04-28 | 2018-05-01 | 2018-05-02 | 2018-05-03 |
|---|---|---|---|---|
| p1 | 50 | 60 | 40 | 42 |
| p2 | NULL | NULL | 4 | 3 |
Quick Tips
- If you want to customize the dynamic column names (e.g.,
stock_20180428instead of2018-04-28), use the Column name pattern intPivotToColumns—try something like"stock_" + Date.format("yyyyMMdd"). - Make sure your source dates are consistent! If there are invalid dates, fix them in the
tMSSqlInputquery or add atFilterRowto catch bad records. - For incremental loads (if you’re running this job regularly), use
tMSSqlOutput’sUpsertmode to update existing rows instead of overwriting everything.
内容的提问来源于stack exchange,提问作者Chifee B'abdesslem

