Try something like this
Also, I think thatCode:Select S.s_id, S.s_name, S.s_symbol, S.s_publisher, S.s_published_date , sum(Case when SD.sd_shares > 0 then SD.sd_shares else 0 end) as sd_shares , case when SD.sd_shares > 0 then (SELECT (SUM(sd_shares * sd_price_per_share) / SUM(sd_shares)) FROM tblStocks_detail) ELSE 0 END As sd_price_per_share , MD.sd_date From dblStocks S Left Join tblStocks_Detail SD On S.s_id = sd.s_id Join ( Select s_id, max(sd_date) as sd_date From tblStocks_Detail Group by s_id ) as MD On (S.s_id = MD.s_id) Group By S.s_id, S.s_name, S.s_symbol, S.s_publisher, S.s_published_date, MD.sd_date
will give you the AVG price per share over the whole database, not just the stock in question (i.e. you'll get the same number in each row).Code:, case when SD.sd_shares > 0 then (SELECT (SUM(sd_shares * sd_price_per_share) / SUM(sd_shares)) FROM tblStocks_detail) ELSE 0 END As sd_price_per_share



LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks