I have data which contains trade transactions from a trading system. There are 2 scenarios found within the data.One scenario is that 3 rows can represent a broker passing cash to an Investment vehicle. Then an Investment vehicle passing the money to a PortfolioThe other is 1 row representing broker going directly to Portfolio hence 1 line. In both cases the 3 rows or 1 row represent a trade package. However in some cases the package number is not available.I am able to work out which rows belong to a particular package by looking at the allocation number, broker and portfolio fields. If a row contains a valid broker and a valid portfolio then that is a package, the 1 row scenario. The valid portfolios and valid brokers are determined if they are contained in the portfolio and broker tableFor the 1 row scenario a unique package number should be added to the row.For the 3 row scenario I use the following criteria-The allocationID within a a package are sequential. -The first row always contains a valid broker-The third row always contain a valid portfolioOnce packages are determined I need to add a package id number to the package. For the 3 row scenario I need to convert the 3 rows into 1 row containing the valid portfolio, valid broker plus the grossAmounts and NetAmounts from the original first line from the package. I have provided some data to further explain the situation.CREATE TABLE #TradeDetail ( AllocationID int IDENTITY(1,1) , Package varchar(12) , [Broker] varchar(50) , Portfolio VARCHAR(20) , GrossAmount_EUR numeric(18,6) , Commission_EUR numeric(18,6) ) CREATE TABLE #Broker ( ID INT IDENTITY (1,1) , [Broker] varchar(50) ) CREATE TABLE #Portfolio ( ID INT IDENTITY (1,1) , [Portfolio] varchar(50) ) INSERT INTO #TradeDetailSELECT '','Good_Broker','Investment_false','120','130' UNION ALLSELECT '','Bad_Broker1','Investment_false','-120','-130' UNION ALLSELECT '','Bad_Broker2','InvestCo','120','130' UNION ALLSELECT '','Good_Broker2','Investment_false','100','150' UNION ALLSELECT '','Bad_Broker4','Investment_false','-100','-150' UNION ALLSELECT '','Bad_Broker5','PortfolioCO','100','150' UNION ALLSELECT '','Good_Broker3','Investment_false','250','100' UNION ALLSELECT '','Bad_Broker6','Investment_false','-250','-100' UNION ALLSELECT '','Bad_Broker7','PortfolioCO2','250','100' UNION ALLSELECT '','Good_Broker3','Investment_false','350','100' UNION ALLSELECT '','Bad_Broker9','Investment_false','-350','-100' UNION ALLSELECT '','Bad_Broker9','InvestCo','350','100' UNION ALLSELECT '','Good_Broker3','Investment_false2','150','100' UNION ALLSELECT '','Bad_Broker11','Investment_false2','-150','-100' UNION ALLSELECT '','Bad_Broker12','InvestmentCO2','150','100' UNION ALLSELECT '','Good_Broker3','InvestmentCO2','100','200' UNION ALLSELECT '','Good_Broker3','InvestCo','400','300' INSERT INTO #Broker SELECT 'Good_Broker' UNION ALLSELECT 'Good_Broker2' UNION ALLSELECT 'Good_Broker3' INSERT INTO #Portfolio SELECT 'Investment' UNION ALLSELECT 'InvestCo' UNION ALLSELECT 'PortfolioCO' UNION ALLSELECT 'PortfolioCO2' UNION ALLSELECT 'PortfolioCO' UNION ALLSELECT 'PortfolioCO' UNION ALLSELECT 'InvestmentCO1' UNION ALLSELECT 'InvestmentCO2'
With the above data the output should be as followsAllocationID Package Broker Portfolio GrossAmount_EUR Commission_EUR__________________________________________________________________________________________________1 UniquepackageID Good_Broker InvestCo 120.000000 130.0000004 UniquepackageID Good_Broker2 PortfolioCO 100.000000 150.0000007 UniquepackageID Good_Broker3 PortfolioCO2 250.000000 100.00000010 UniquepackageID Good_Broker3 InvestCo 350.000000 100.00000013 UniquepackageID Good_Broker3 InvestmentCO2 150.000000 100.00000016 UniquepackageID Good_Broker3 InvestmentCO2 100.000000 200.00000017 UniquepackageID Good_Broker3 InvestCo 400.000000 300.000000