I need to create a query that can partition a data set and the do some updates to the partitions and finally produce an output.In my data set are rows that pertain to individual trade transactions. A package is made up of several transactions. Each transaction contains a broker, portfolio, commission and gross amount. Within each transaction there can be placeholder brokers and place older portfolios. i.e.values that are used as dummies to fill fields in the source entry system. For each package there can only be on real broker My query needs to pull out all the rows(transactions) per package which have a real portfolio and then for those lines update the broker field with correct/real broker for that package. The way to determine if a broker or portfolio is real is to check if it exists in the portfolio and broker table. I have provided some sample data for clarity. CREATE TABLE #TradeDetail ( 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 #TradeDetail--drop table #TradeDetail SELECT '123456','Good_Broker','Investment','120','130' UNION ALLSELECT '123456','Bad_Broker1','Investment_false','100','130' UNION ALLSELECT '123456','Bad_Broker2','InvestCo','120','100' UNION ALLSELECT '123456','Bad_Broker3','Investment_false','100','130' UNION ALLSELECT '123456','Bad_Broker4','Investment_Cash','100','150' UNION ALLSELECT '123456','Bad_Broker5','PortfolioCO','150','100' UNION ALLSELECT '789110','Good_Broker2','InvestmentCO2','150','100' UNION ALLSELECT '789110','Bad_Broker6','Investment_false','150','100' UNION ALLSELECT '789110','Bad_Broker7','PortfolioCO2','150','100' UNION ALLSELECT '789110','Bad_Broker8','Investment_Cash','150','100' UNION ALLSELECT '789110','Bad_Broker9','PortfolioCO','150','100' UNION ALLSELECT '453211','Good_Broker3','InvestCo','150','100' UNION ALLSELECT '453211','Bad_Broker10','InvestmentCO1','150','100' UNION ALLSELECT '453211','Bad_Broker11','Investment_false2','150','143' UNION ALLSELECT '453211','Bad_Broker12','InvestmentCO2','105','200' UNION ALLSELECT '453211','Bad_Broker13','Investment_false','100','200' UNION ALLSELECT '453211','Bad_Broker14','Investment','100','220' 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 this data the output should be:Package Broker Portfolio GrossAmount_EUR Commission_EUR123456 Good_Broker Investment 120 130 123456 Good_Broker InvestCo 120 100123456 Good_Broker PortfolioCO 150 100789110 Good_Broker2 InvestmentCO2 150 100789110 Good_Broker2 PortfolioCO2 150 100789110 Good_Broker2 PortfolioCO 150 100453211 Good_Broker3 InvestCo 150 100453211 Good_Broker3 InvestmentCO1 150 100453211 Good_Broker3 InvestmentCO2 105 200453211 Good_Broker3 Investment 100 220 [/code]