求Excel或SQL实现表格列转行的ETL转置与交叉连接方案
Got it, let's tackle this problem thoroughly. You need to reshape 10 Wk-named columns (e.g., Wk1 to Wk10) into two columns—Week and Value—and build a complete ETL pipeline for this process. I'll cover both Excel-based workflows (great for small datasets or manual tasks) and SQL-based approaches (ideal for database-integrated, scalable pipelines), then wrap everything into a full ETL flow.
Power Query is the most efficient way to handle this reshaping in Excel, especially for batch processing. Here's how to do it:
Step 1: Import your data into Power Query
Open your Excel file, go to the Data tab → Get Data → From File → From Workbook. Select your file, choose the target worksheet, and click Load To → Only Create Connection. Then, go back to Data → Connections, right-click the new connection, and select Edit.Step 2: Unpivot the Wk columns
In the Power Query Editor, select all non-Wk columns (like IDs, product names, or other dimension columns you want to keep). Then go to the Transform tab → Unpivot Columns → Unpivot Other Columns. This will turn all Wk columns into two new columns:Attribute(containing Wk1, Wk2, etc.) andValue.Step 3: Clean up the Week column
Select theAttributecolumn, go to Transform → Replace Values. Replace "Wk" with an empty string, then rename the column toWeek.Step 4: Export the transformed data
Click Close & Load to bring the reshaped data back to a new Excel worksheet, or export it as a CSV/Excel file to prepare for database import.
Bonus: Formula-Based Method (For Quick Small-Scale Tasks)
If you don't want to use Power Query, you can use formulas:
- In a new
Weekcolumn:=RIGHT(A1, LEN(A1)-2)(assuming A1 is a Wk column header; adjust the cell reference as needed) - In the corresponding
Valuecolumn:=A1(link to the original Wk cell) - Drag the formulas down to cover all rows, then copy-paste values to lock in the data. Note: This is less efficient for large datasets.
Once your Excel data is imported into a database table (let's assume the table is named raw_weekly_data with columns like id, product, Wk1 through Wk10), use these methods to reshape it:
Universal UNION ALL Approach (Works for All SQL Databases)
This method is compatible with MySQL, PostgreSQL, SQL Server, etc.:
SELECT id, product, '1' AS Week, Wk1 AS Value FROM raw_weekly_data UNION ALL SELECT id, product, '2' AS Week, Wk2 AS Value FROM raw_weekly_data UNION ALL -- Repeat this pattern for Wk3 to Wk9 SELECT id, product, '10' AS Week, Wk10 AS Value FROM raw_weekly_data -- Optional: Filter out rows with empty values WHERE Value IS NOT NULL;
SQL Server UNPIVOT Method (More Concise)
If you're using SQL Server, the UNPIVOT operator simplifies the query:
SELECT id, product, REPLACE(week_col, 'Wk', '') AS Week, Value FROM raw_weekly_data UNPIVOT ( Value FOR week_col IN (Wk1, Wk2, Wk3, Wk4, Wk5, Wk6, Wk7, Wk8, Wk9, Wk10) ) AS unpvt WHERE Value IS NOT NULL;
Here's an end-to-end flow to extract, transform, and load your data:
1. Extract (Data Extraction)
- Source Data Read: For manual workflows, open the Excel file directly. For automation, use tools like Python's
pandas(script), Apache Airflow, or SSIS to read the Excel worksheet programmatically. - Validation: Verify that all 10 Wk columns exist, and check sample rows to ensure data types (e.g., numeric values in Wk columns) are correct.
2. Transform (Data Transformation)
- Choose Your Tool:
- Use Excel Power Query for manual/small datasets.
- Use SQL for database-integrated workflows (run the query above after importing raw data).
- Use Python
pandasfor automated scripting:import pandas as pd # Read raw Excel data df = pd.read_excel('your_raw_data.xlsx', sheet_name='Sheet1') # Reshape the data (unpivot Wk columns) transformed_df = pd.melt( df, id_vars=[col for col in df.columns if not col.startswith('Wk')], var_name='Week', value_name='Value' ) # Clean up the Week column transformed_df['Week'] = transformed_df['Week'].str.replace('Wk', '') # Remove rows with empty values transformed_df = transformed_df.dropna(subset=['Value']) # Save transformed data transformed_df.to_excel('transformed_weekly_data.xlsx', index=False)
- Validation: Confirm the transformed data has
Weekvalues from 1-10,Valuematches the original Wk columns, and row count is as expected (original rows ×10 minus empty value rows).
3. Load (Data Loading)
- Excel to Database: Use your database's import tool (e.g., MySQL Workbench Import Wizard, SQL Server Import/Export Data) to load the transformed Excel/CSV file into a target table (ensure the target table has columns for
Week,Value, and any other dimension columns). - SQL Direct Load: If using the SQL transformation query, insert the results directly into your target table:
INSERT INTO transformed_weekly_data (id, product, Week, Value) SELECT id, product, REPLACE(week_col, 'Wk', '') AS Week, Value FROM raw_weekly_data UNPIVOT ( Value FOR week_col IN (Wk1, Wk2, ..., Wk10) ) AS unpvt WHERE Value IS NOT NULL; - Validation: Query the target table to confirm row counts, values, and data types match the transformed dataset.
内容的提问来源于stack exchange,提问作者eTothEipiPlus1

