Computed column in view VS trigger on update

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 1
Reputation: waolly is an unknown quantity at this point 
Solved Threads: 0
waolly waolly is offline Offline
Newbie Poster

Computed column in view VS trigger on update

 
0
  #1
Sep 7th, 2006
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!
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Computed column in view VS trigger on update

 
0
  #2
Sep 7th, 2006
Use SQL Servers AVG keyword/function

  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)
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC