Store calculations in a table or use computed columns?

  • I'm working on a Silverlight / WCF project and I'm now in need of some SQL guidance from you people that know SQL inside out.

    I have reached a point in the project where I need to give the user the ability to create certain calculations that is going to be used for validating all the data that goes into the database.

    To give a short explanation:

    - Table A contains data concerning electrical consumption

    - Table B contains data concerning heat consumption

    - The user adds a calculation [Table A].[Column 1] - [Table B].[Column 4] and a max value that this calculation may have. A service would later use this calculation to match it against the set max-value.

    From my POW, this can me done in two ways, either store the calculations instruction-wise (eg. a table with all the necessary details that is needed for a service to fetch all the needed data and do the calculations off-site) OR one could create a view with computed columns which does this automatically.

    Can someone please give me some pointers in which direction I should be going? Should I implement this the view-way (generate views through my application that does the calculations) or should I somehow store the instructions for the calculations in a table and use a service which does all the calculations and checks (would poll the database a lot more than going the view way).

    Are there any other alternatives which I should take into consideration? Has anyone encountered this before and how have you solved it?

    /Tobias

  • the devil is in the details, but here's my two cents:

    use computed columns or a view which does the computation for you whenever possible.

    i don't know if you've ever tried it, but storing a formula in a field, and then trying to execute the value of the field to get the results is a real pain in the... whatever.

    try it with a simple example...

    declare @MyCalculation varchar(30)

    SET @MyCalculation ='SELECT 1 + 2+ 3'

    EXEC (@MyCalculation)

    --or worse COLUMNA + COLUMNB

    see if you can get the results back...it's a pain.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Thanks Lowell, I'll start working on using computed columns.

    If anyone else pictures an even better way when reading this post, please share your ideas 🙂

  • As Yogi Bera used to say, "When you come to a fork in the road, take it.". If you can suscessfully create a computed column, try to make it a "persisted" computed column.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply