Link to home
Start Free TrialLog in
Avatar of jzimmerman
jzimmermanFlag for United States of America

asked on

Dynamic T-SQL Math

I have a table with customer number, name, and several numbers.  Depending on the customer number I need to be able to add some/all of the number fields up.  Every customer could be different.  I could write a .Net app to process each customer one by one, SLOW.  Is there any way to design this so T-SQL can do all the work?  I am open to any suggestions, stored procedures, temp tables, case statements, etc.  The table that will hold how to calculate the total is not defined yet, I am open to any ideas.
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

well, until now, the process does not seem very clear yet.
can you explain that a bit more concrete?
Avatar of jzimmerman

ASKER

The only concrete thing is I have a table with many numeric fields and want to add them together differently.

The storage of the rules will depend on the solution.
??? Well, as you might be aware, we don't see your screen, and cannot read your mind.
with what I read until here, I am completely unaware of what you are trying to achieve...
please give CONCRETE samples (demo data...)
Table:
   AN, AC, N1, N2, N3
   name 1, 123, 1, 2, 3
   name 2, 321, 3, 2, 2


if(AN == 'name 1) N1 + N2 + N3
if(AN == 'name 2' N1 + N3 - N2
etc.....
You could create a computed/virtual column that matches each unique formula, if you are willing to "clog" up the table definition like that.  This, to me, is the fastest, easiest and, most importantly, guaranteed accurate way to do this.

There are limits to the number of columns a table can have, however.  Hopefully you wouldn't exceed that.
If you are willing to consider that and want for info, just let me know.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The only idea I had was to add 2 fields – formula and result.  Then use a cursor to exec([the update sql statement]) and do a select on the result field.  I was hopping some one would have a better idea.  So, thanks anyways.
So you don't want to use computed columns and a function that is recreated using dynamic SQL?
Avatar of Gautham Janardhan
Gautham Janardhan


1) As angel suggested u can do ur computation within ur select query
2) or use scottPletchers suggestion to use computed columns.
Hmm, have to admit I don't understand how that would work.  How do you know whether to add / subtract / multiply / divide ?

as I said, the assumption would be that only add / substract would be possible.
the coefficient of 1 would add, -1 would substract, and 0 would remove the value from the "formula"

as by the 2 fields formula and result:
I would then develop a COM DLL using some other tool which could take the data as input, and return the results using an exising eval() function, and call that from a trigger for example... (sp_OACreate ...)