Solved

Dynamic T-SQL Math

Posted on 2006-11-03
15
869 Views
Last Modified: 2008-01-09
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.
0
Comment
Question by:jzimmerman
  • 4
  • 4
  • 3
  • +1
15 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17866883
well, until now, the process does not seem very clear yet.
can you explain that a bit more concrete?
0
 

Author Comment

by:jzimmerman
ID: 17867030
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.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17867131
??? 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...)
0
 

Author Comment

by:jzimmerman
ID: 17867782
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.....
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 17868454
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.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 17868472
If you are willing to consider that and want for info, just let me know.
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
ID: 17868513
what kind of formulas would you have? only additions/substractions?

yes:
i would probably define then a table with a NAME column and the same number of C1, C2 ... columns which would be the coefficient, and the query would be like this:

select t.ac, t.a1 * c.c1 + t.a2 * c.c2 + ... etc
from yourtable t
join coefficients c
  on c.name = t.AN

Hope this helps
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 250 total points
ID: 17868558
Hmm, have to admit I don't understand how that would work.  How do you know whether to add / subtract / multiply / divide ?
0
 

Author Comment

by:jzimmerman
ID: 17868946
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.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 17868959
So you don't want to use computed columns and a function that is recreated using dynamic SQL?
0
 
LVL 29

Expert Comment

by:Gautham Janardhan
ID: 17871988

1) As angel suggested u can do ur computation within ur select query
2) or use scottPletchers suggestion to use computed columns.
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17872366
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 ...)
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tempdb latch contention 12 50
datetime in sql 6 29
MS SQL combine two tables by splitting codes in one table into multiple values 6 34
MS SQL Pivot table help 4 14
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now