473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cacluated Field

I am designing an object model and DB and I can't decide where to put
calculated fields... Should it be in the database or the middle-tier?

In other words, if I have an OrderItem on an Order and there are two
columns called "Quantity" and "Cost" I also will want "TotalCost" =
"Quantity x "Cost".... Do I have the "GET" stored proc return this
caculcated value or just create the property in my middle-tier and have
it calculate it there?

Opinions anyone?

Jul 23 '05 #1
5 1201

<jo**@nautilusnet.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I am designing an object model and DB and I can't decide where to put
calculated fields... Should it be in the database or the middle-tier?

In other words, if I have an OrderItem on an Order and there are two
columns called "Quantity" and "Cost" I also will want "TotalCost" =
"Quantity x "Cost".... Do I have the "GET" stored proc return this
caculcated value or just create the property in my middle-tier and have
it calculate it there?

Opinions anyone?


For such a simple calculation, I would do it in either the stored procedure
or perhaps a view - the view is useful if you want to have the value
available to other clients which may not always call your proc, and for ad
hoc queries. You could use a computed column as well, but I haven't used
them much, so I don't really know how they perform with large data sets.

More complex calculations may be better in the middle tier, especially if
you only calculate for relatively small data sets, and if you need to use
mathematical functions which aren't available in TSQL, then you may have no
choice. As in many cases, the best way to get a good answer is to test it
yourself with your own data and see which solution works out better for you.

Simon
Jul 23 '05 #2
jo**@nautilusnet.com wrote:
I am designing an object model and DB and I can't decide where to put
calculated fields... Should it be in the database or the middle-tier?

In other words, if I have an OrderItem on an Order and there are two
columns called "Quantity" and "Cost" I also will want "TotalCost" =
"Quantity x "Cost".... Do I have the "GET" stored proc return this
caculcated value or just create the property in my middle-tier and have
it calculate it there?

Opinions anyone?


First off, I would highly suggest that you have all of these calculated
fields defined in some sort of data dictionary so that you can use a code
generator of some sort to generate it. This lets you change your mind
about implementation details after the fact.

Calculated fields can come down to these types:

1) EXTEND, most common, extended = price * qty
2) FETCH, pull price from items table into orders, trigger action is change
of value of order_detail.item_code.
3) AGGREGATE, any sum, avg, min, max or count() from detail to header
4) DISTRIBUTE, like a fetch, in that a value goes from header to detail,
but triggering action is a change in value in header, and it is pushed to
*all* rows in child table that match on pk/fk. Included for completeness
but considered evil.

All approaches boil down to either materializing in the tables, or not doing
it.

The simplest approach if you don't put them into tables is to create views.
I've done this with a view generator and it is pretty nifty. The danger is
that the very simplicity of the views will obscure very deeply nested
subqueries, which may not be discovered until the system comes under heavy
load.

The other option is to materialize them into the tables. This is considered
evil by relational theorists, but the only real requirement if you do this
is that you not let a casual user update the automated columns. So a
straight command "UPDATE ... SET TotalCost=5 " should fail with an error.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 23 '05 #3
I am just now getting back to this thread. In my opinion the problem
with putting calculations into the the sproc is that it's not possible
to no the calculation until call the sproc again. This feels unnatural
when working with an object model, for example:

OrderItem item = new OrderItem();
item.Quantity = 5;
item.Cost = 10.00;

Response.write(item.Total);

For the above code to work using the "calculations in sproc" method I
would have to hit the database again for item.Total to have a value...

Jul 23 '05 #4
If you want to see the calculated total immediately, then a view is
probably better than a procedure:

create view dbo.OrdersWithTotalCost
as
select
OrderID,
OrderItemID,
...
/* Other columns from Orders */
...
Quantity,
Cost,
Quantity * Cost as 'Total'
from
dbo.Orders

I'm not sure I understand your concern about hitting the database again
- since your calculation is so simple, you will know the value of Total
before you even INSERT the new order item, and you may not need to
retrieve it again (unless there's further processing in the database,
of course).

If you really want to avoid another query, then one option is to create
an InsertOrderItem stored procedure, which INSERTs the new item and
then returns the total as an output parameter.

Simon

Jul 23 '05 #5
Simon Hayes wrote:
If you want to see the calculated total immediately, then a view is
probably better than a procedure:

create view dbo.OrdersWithTotalCost
as
select
OrderID,
OrderItemID,
...
/* Other columns from Orders */
...
Quantity,
Cost,
Quantity * Cost as 'Total'
from
dbo.Orders

I'm not sure I understand your concern about hitting the database again
- since your calculation is so simple, you will know the value of Total
before you even INSERT the new order item, and you may not need to
retrieve it again (unless there's further processing in the database,
of course).

If you really want to avoid another query, then one option is to create
an InsertOrderItem stored procedure, which INSERTs the new item and
then returns the total as an output parameter.


My original suggestion held that the definitions should be stored in a data
dictionary and any implemention, views or sprocs, should be generated from
that.

If you do that, the client (some OO code) can read the dictionary, or you
can generate code for classes, and they can do their own calculations on
the fly for user convenience. You then can independently decide how to
implement the same formulas on the server.

If the implementation is based on a dd, you can try different methods and
change your mind rather painlessly.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Randell D. | last post by:
Folks, Perhaps someone can figure this out - this is 'the process of my script' I have a form whereby I can add multiple contacts to a single address. There is only one...
5
by: ND | last post by:
I need to create a separate field from 4 fields, "street address", "city", "State" and "zip code". For example, Street address - 100 Forest Street City - Seattle State - WA Zip - 05555 ...
26
by: temp | last post by:
Hi, My boss is asking me to generate a column mapping report of all the queries. Basically, we get our data from ORACLE. There's a queary that create new table from ORACLE tables. Then, there...
18
by: Dixie | last post by:
Can I set the Format property in a date/time field in code? Can I set the Input Mask in a date/time field in code? Can I set the Format of a Yes/No field to Checkbox in code? I am working on...
2
by: Brett | last post by:
My database has 2 tables: Table1 & Table2. If a field is not null on a record in table2, then the not null fields in table1 that correspond to the records in table1 needs to be updated to match the...
9
by: sellcraig | last post by:
Microsoft access 2 tables table "data main" contains a field called "code" table "ddw1" is created from a make table query of "data main" Goal- the data in "code" field in needs to...
13
by: Lee | last post by:
I have this function that doesn't work. I pass it the td element and an id, and it makes an input field inside the td. That part workds. What doesn't work is that I want to add an "onkeyup" on...
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
1
by: roveagh1 | last post by:
Hi I've been using the 2 year old link below to repeat values from previous record field into current corresponding field. It's worked fine for text but the last piece of advice was to use the same...
3
by: klbachrodt | last post by:
Hi all - I've been browsing this forum for a while now, looking for help, and finally decided to join and ask my question, since I'm not terribly good at writing SQL queries in Access. Here is my...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.