使用SAS Proc Transpose转置数据集时避免金额重复计数的问题
Fixing Account Transposition to Avoid Duplicate Amounts
Got it, let's fix this transposition issue so each account only shows its own amount without duplication—this way your totals stay accurate even with large datasets. Here are solutions for common tools you might be using:
SQL Solution
If you're working with a database (like PostgreSQL, MySQL, SQL Server), this approach splits your original table into two separate sets of records (one for each account column) then combines them, filling the unused amount column with 0.
-- Replace 'your_table' with your actual table name SELECT Account1Amount AS Amount1, 0 AS Amount2, Account1 AS Col1 FROM your_table UNION ALL SELECT 0 AS Amount1, Account2Amount AS Amount2, Account2 AS Col1 FROM your_table ORDER BY Col1;
How this works:
- The first
SELECTpulls all records fromAccount 1and their corresponding amounts, settingAmount2to 0. - The second
SELECTdoes the same forAccount 2, settingAmount1to 0. UNION ALLcombines both result sets (useUNIONonly if you need to remove duplicates, butUNION ALLis faster for large data).- The
ORDER BYsorts the final result by the account name (Col1) for readability.
Python Pandas Solution
For large datasets processed in Python, Pandas makes it easy to split, transform, and combine the data:
import pandas as pd # Sample input data (replace with your actual DataFrame) df = pd.DataFrame({ 'Account 1': ['A', 'F'], 'Account 2': ['B', 'D'], 'Account1Amount': [25, 10], 'Account2Amount': [55, 70] }) # Split into two separate DataFrames for each account type df_account1 = df[['Account 1', 'Account1Amount']].rename( columns={'Account 1': 'Col1', 'Account1Amount': 'Amount1'} ) df_account1['Amount2'] = 0 df_account2 = df[['Account 2', 'Account2Amount']].rename( columns={'Account 2': 'Col1', 'Account2Amount': 'Amount2'} ) df_account2['Amount1'] = 0 # Combine and clean up the result final_df = pd.concat([df_account1, df_account2]).sort_values('Col1').reset_index(drop=True) print(final_df)
Output:
Col1 Amount1 Amount2 0 A 25 0 1 B 0 55 2 D 0 70 3 F 10 0
Excel Solution
If you prefer using Excel (especially for smaller datasets or manual workflows):
Manual Approach:
- Create a new table with columns
Amount1,Amount2,Col1. - Copy all values from
Account 1intoCol1, their correspondingAccount1AmountintoAmount1, and fillAmount2with 0. - Below those rows, copy all values from
Account 2intoCol1, their correspondingAccount2AmountintoAmount2, and fillAmount1with 0. - Sort the table by
Col1to organize the accounts.
Automated Power Query Approach (for large datasets):
- Load your data into Excel Power Query (Data > Get Data > From Table/Range).
- Duplicate the query twice (right-click the query > Duplicate).
- For the first duplicate:
- Keep only
Account 1andAccount1Amountcolumns. - Rename columns to
Col1andAmount1. - Add a custom column
Amount2with value 0.
- Keep only
- For the second duplicate:
- Keep only
Account 2andAccount2Amountcolumns. - Rename columns to
Col1andAmount2. - Add a custom column
Amount1with value 0.
- Keep only
- Merge the two queries (Home > Append Queries > Append Queries as New).
- Sort the appended table by
Col1, then load it back into Excel.
内容的提问来源于stack exchange,提问作者mustafghan
相关产品推荐
相关产品推荐

