带有count和group by的SQL视图

时间:2014-02-18 09:25:01

标签: sql sql-server

我有表AddressPropertyListing

Create Table Listing
    ( PropertyID int  -- Property ID as per the Property table
    , AgentID int
    , ListingDate DateTime not null property
    , AskingPrice Decimal(10,2) not null 
    , SaleDate Date
    , SalePrice Decimal(10, 2)  
    , Primary Key (PropertyID, ListingDate)
    , Foreign Key (PropertyID) references Property(PropertyID)
    , Foreign Key (AgentID) references Agent(AgentID) on delete no action on update no action

Create Table Address
    ( AddressID int Primary Key
    , StreetAddress varchar (100)
    , City varchar (50)
    , StateCode char(3)
    , PostalCode char (12)
    , Country varchar(30)
    )
        )
Create Table Property 
    ( PropertyID int Primary Key -- Unique ID for each property
    , AddressID int references Address(AddressID) On Delete no action on update no action
    , NumberOfRooms int not null Check (NumberOfRooms > 0) -- Number of rooms
    )

我想创建一个视图,其中包含每个城市的待售物业数量及其平均价格。如果AskingPrice不是,则可以出售该属性,SaleDate = null。问题是我无法获得每个城市的计数,因为我收到了错误

  

每个GROUP BY表达式必须至少包含一个不是外部引用的列

我该如何解决这个问题?

我的代码:

create view MarketStatistics as
select City = a.City,
     Properties = (select count(PropertyID)from Listing l where l.AskingPrice is not Null and l.SaleDate is Null group by a.City),
     AskingPrice = (select avg(AskingPrice)from Listing)
from Address a
join Property p on p.AddressID = a.AddressID
join Listing l on p.PropertyID = l.PropertyID

5 个答案:

答案 0 :(得分:5)

您的查询可以更加简单,只需在不使用子查询的情况下进行分组:

select 
    a.City,
    count(*) as Properties,
    avg(l.AskingPrice) as AskingPrice 
from Address a
inner join Property p on p.AddressID = a.AddressID
inner join Listing l on p.PropertyID = l.PropertyID
where l.AskingPrice is not Null and l.SaleDate is Null
group by a.City

答案 1 :(得分:1)

我认为你不需要分组。简单的联合相关查询应该有效:

 create view MarketStatistics as
 select City = a.City,
 Properties = (select count(PropertyID)from Listing l join Property p on p.PropertyID = a.PropertyID where l.AskingPrice is not Null and l.SaleDate is Null and p.addressID = a.addressID),
 AskingPrice = (select avg(AskingPrice)from Listing l join Property p on p.PropertyID = a.PropertyID where p.addressID = a.addressID)
 from Address a

我假设您需要为所有房产询价,因为您没有在查询中对要价进行空检查。

答案 2 :(得分:0)

尝试在子查询中添加另一列,而不是外部引用,如:

create view MarketStatistics as
select City = a.City,
     Properties = (select count(PropertyID)from Listing l where l.AskingPrice is not Null and l.SaleDate is Null group by a.City, PropertyID),
     AskingPrice = (select avg(AskingPrice)from Listing)
from Address a
join Property p on p.AddressID = a.AddressID
join Listing l on p.PropertyID = l.PropertyID

答案 3 :(得分:0)

试试这个

  create view MarketStatistics as
    select City = a.City,
    Properties = (select count(PropertyID)from Listing l where l.AskingPrice is not Null   and l.SaleDate is Null),
    AskingPrice = (select avg(AskingPrice)from Listing)
    from Address a
    join Property p on p.AddressID = a.AddressID
    join Listing l on p.PropertyID = l.PropertyID
    group by a.City

答案 4 :(得分:0)

group by必须至少包含select statement中的一个列名 因此,请将sub-query更改为

select count(PropertyID)
from Listing l where l.AskingPrice is not Null 
     and l.SaleDate is Null 
group by a.City, PropertyID