In simple terms I want to add the contents of some columns together but only if the associated columns contain a specific value. To do this in MS Access I use the following:
STRUint:(Abs(1*([Code01]='0192'))*[Code01int])+(Abs(1*([Code02]='0192'))*[Code02int])...
Breaking this down, if Code01='0192' I get the result TRUE which I multiply by 1 to get -1 and use Abs to convert to 1. This is then multiplied by Code01int, so I'd have an integer equal to Code01int only when Code01 was '0192' otherwise I'd have zero. The result of that calculation is added to the next calculation in the sequence and so on.
The sequence is repeated 15 times adding together the integers in Code01int through Code15int where the content of Code01 through Code15 is '0192'
The only way I personally can think how to do this in SQL is with
UPDATE Table1
SET STRUint = 0
UPDATE Table1
SET STRUint = CASE
WHEN Code01='0192' THEN STRUint + Code01int
END
UPDATE Table1
SET STRUint = CASE
WHEN Code02='0192' THEN STRUint + Code02int
END
etc
Is this the only way to achieve my goal, by doing many UPDATEs? I do this for 15 sets of columns and repeat for different string content 5 times, so 80 updates (including the initial set to zero).
I'm hoping there's a way to do this without so many updates on the assumption that it's better to not do so many.
What's wrong with:
UPDATE Table1 SET STRUint = 0;
UPDATE
Table1
SET
STRUint +=
CASE WHEN Code01='0192' THEN Code01int ELSE 0 END
+ CASE WHEN Code02='0192' THEN Code02int ELSE 0 END
+ CASE WHEN Code03='0192' THEN Code03int ELSE 0 END
etc.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments