944,131 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 7001
  • MS SQL RSS
Sep 7th, 2006
0

Computed column in view VS trigger on update

Expand Post »
Hi guys,

Let me describe the situation I am in. I have a table of 2000 objects and each object has a range of different prices associated with it, stored in another table.

I have created a view which serves to list the objects as well as their average price. At present I have something along the lines of

CREATE VIEW objview AS
select o.*, (select sum(op.price)/count(*) from objprice op where op.objectid=o.objectid) averageprice
from obj o

I have a feeling that the above might not be very efficient.

Does anyone have any input regarding speed and efficiency on
- using a join in the above instead of a nested query
- having a fixed field in the original obj table which gets updated via a trigger when new values are inserted into objprice

Which is the best solution?

Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
waolly is offline Offline
1 posts
since Sep 2006
Sep 7th, 2006
0

Re: Computed column in view VS trigger on update

Use SQL Servers AVG keyword/function

MS SQL Syntax (Toggle Plain Text)
  1. SELECT
  2. o.column1, o.column2, ...
  3. avg(p.price)
  4. FROM
  5. Objecttable o
  6. JOIN Pricetable p ON o.id = p.id
  7. WHERE
  8. <put criteria IF needed otherwise leave WHERE out>
  9. GROUP BY
  10. o.column1, 0.column2, .... (but NOT the COLUMN IN the avg() expr)
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Modal Function
Next Thread in MS SQL Forum Timeline: SQL query





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC