MsSql查找平均值列表的最大值

时间:2014-07-01 05:18:38

标签: sql sql-server view

iv刚刚开始使用SQL,说实话,我现在正专心致志...... 我有一个房地产数据库的任何方式,我需要显示城市的名称,邻居和该社区的销售利润,直到最昂贵的社区,直到现在。

由于某种原因,选择在利润列显示最大的存量,但也显示所有的社区.... 这是iv完成的事情:

create view V_Avrage_price as
select 'avgprice'=avg(s.actualprice),c.CityName, n.name
from sale s, Neighborhood n, city c, house h
where n.CityID=c.CityID and h.SaleID=s.SaleID and h.NeighborhoodID=n.NeighborhoodID
group by n.Name, c.CityName


select distinct c.CityName,n.name, avgprice
from V_Avrage_price, city c, Neighborhood n, sale s, house h
where n.CityID=c.CityID and h.SaleID=s.SaleID and h.NeighborhoodID=n.NeighborhoodID
and avgprice=(select max(avgprice) from V_Avrage_price) 

2 个答案:

答案 0 :(得分:0)

select 'avgprice'=avg(s.actualprice),c.CityName, n.name 
from sale s, Neighborhood n, city c, house h 
where n.CityID=c.CityID and h.SaleID=s.SaleID and h.NeighborhoodID=n.NeighborhoodID 
group by n.Name, c.CityName

select distinct c.CityName,n.name, avgprice 
from V_Avrage_price, city c, Neighborhood n, sale s, house h 
where n.CityID=c.CityID and h.SaleID=s.SaleID and  h.NeighborhoodID=n.NeighborhoodID and avgprice=(select max(avgprice) from V_Avrage_price)

答案 1 :(得分:0)

我认为这已经足够了

select distinct CityName,name, avgprice
from   V_Avrage_price
where avgprice=(select max(avgprice) from V_Avrage_price)