|
helpme
Posting Yak Master
141 Posts |
Posted - 2009-08-25 : 10:21:21
|
| I need to update tbl1, column newamt1 based on information in tbl2.Here's the processing steps:1) only update records in tbl1 where val1a = 'A ' or val1a = ' A'2) for the records in step 1, retrieve amt2 from tbl2 based on year1 (year2), code1a (code2a) and code1b (code2b) from tbl1. Also, val2a and val2b must be = 'A ' or ' A'.3) if a match isn't found in step 2, try again retrieving amt2, matching only on year1 (year2) and code1a (code2a) from tbl2 (code2b = space). again, val2a and val2b must be = 'A ' or ' A'4) for records in step 1, retrieve amt2 from tbl2 based on year1 (year2), code1a (code2a) and code1b (code2b). on these, val2a must be = 'A ' or ' A', but val2b must be '01' or higher.5) if a match isn't found in step 4, try again retrieving amt2, matching only on year1 (year2) and code1a (code2a) from tbl2 (code2b = space). val2a must = 'A ' or ' A'. val2b must be 01 or higher.6) if an amt2 is retrieved from tbl2 in step 4 or 5 above, divide the result from step 2 (or step 3 if no result found in step 2) with the result from step 4 (or step 5, if no result found in step 4).7) if the result from step 6 is less than .5, use .5, else use result from number step 6. with this percentage, update tbl1 (column newamt1) by setting it equal to column amt1 multiplied by this percentage. Odd as this sounds, I am dealing with a situation like this. Didn't know if it could be done without a cursor or not.-----------------------------------TABLESCREATE TABLE [dbo].[tbl1] ( [year1] [int] NULL , [code1a] [varchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [code1b] [varchar] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [val1a] [varchar] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [amt1] [numeric](10, 2) NULL , [newamt1] [numeric](10, 2) NULL ) ON [PRIMARY]CREATE TABLE [dbo].[tbl2] ( [year2] [int] NULL , [code2a] [varchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [code2b] [varchar] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [val2a] [varchar] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [val2b] [varchar] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [amt2] [numeric](10, 2) NULL ) ON [PRIMARY]-----------------------------------DATATBL12007,'0001','001','01',25,02007,'0001','001','01',50,02007,'0001','001','A ',75,02007,'0001','001',' A',25,02007,'0001','001','05',50,02007,'0002','002','A ',75,02007,'0002','002','03',25,02008,'0001','002',' A',75,02008,'0001','002','02',50,02008,'0003','003','A ',150,0TBL22007,'0001','001','A ','A ',1002007,'0002',' ',' A',' A',2002007,'0002','001',' A','A ',2222007,'0001','001','A ','05',3002007,'0002',' ','01','05',7002007,'0002','001','01','05',7002008,'0001','002','03','05',9002008,'0003','003','A ','A ',2002008,'0003','003','01','02',350-----------------------------------EXPECTED RESULTSTBL1 (records number 3,4,6 and 10 updated)2007,'0001','001','01',25,02007,'0001','001','01',50,02007,'0001','001','A ',75,37.52007,'0001','001',' A',25,12.52007,'0001','001','05',50,02007,'0002','002','A ',75,37.52007,'0002','002','03',25,02008,'0001','002',' A',75,02008,'0001','002','02',50,02008,'0003','003','A ',150,85.5------------------------------------ |
|