条件Sql查询

San*_*nju 5 sql database sql-server-2008

我正在显示下表中的属性.现在我要做的是,在相同的位置找到属性(假设我的属性位于sec-19,匹配sec-19,如果没有找到那里,那么搜索整个城市)具有以下条件:它应该在10天前发布,或者如果没有在10天后发布,则比30天后的结果更新.

我有下面提到的表(属性):

ID | Propertyid | Userid | Projectid | .. |价格| ... |上市时间| ... http://www.freeimagehosting.net/uploads/1e5ee2e2ad.jpg

现在我要从这个表中检索的是那些列出时间少于10天的属性的'Propertyid'和'Average Price',如果它们都不少于10天,那么返回结果的时间少于30天.

任何人都可以帮我解决这个问题.提前致谢.

或者只是任何身体都可以在没有位置匹配的情况下回答

我需要从10天前发布的房产计算'平均价格',如果10天前没有贴出房产,那就把它当作30天前.像这样的东西:

Select AVG(Price) As Average_Price from Properties where (DATEDIFF(day,listingtime,getdate())<30 or DATEDIFF(day,listingtime,getdate())<10)
Run Code Online (Sandbox Code Playgroud)

但在这里,我只得到一个字段'平均价格',这里我也没有检查过滤它是在10天前还是30天前发布的.Knidly Recheck并尝试解决我的问题.提前致谢.

awr*_*t18 1

我花了一些时间在这个问题上,我相信我解决了您所有的担忧。我不完全确定城市或位置的数据类型,因此我使用了 varchar(100) 这应该可以解决您的所有问题。如果您描述的情况无法解决,请发表评论。

   CREATE PROCEDURE [dbo].[GetRecentlyListedProperties]
(@location varchar(100), @city varchar(100),@propertyID int)
As
Begin
DECLARE @numberOfDays int,
        @propertyCount int, 
        @IsLocation bit -- looking for a location and not a city    
SET @Propertycount = 0
SET @numberOfDays= 10 
-- CHECK TO SEE IF THERE ARE LISTINGS IN THE LAST 10 DAYS IN THE SAME LOCATION
SELECT  @PropertyCount = 
 Count(*) FROM properties where location = @location and DATEDIFF(day,listingtime,GETDATE()) < @numberOFDays
 and PropertyID != @propertyID
If(@PropertyCount = 0)
Begin
-- CHECK TO SEE IF THERE ARE LISTINGS IN THE LAST 10 DAYS IN THE SAME CITY
SELECT  @PropertyCount = Count(*) from properties where city = @city 
        AND DATEDIFF(day,listingtime,GETDATE()) < @numberOFDays
        AND PropertyID != @propertyID   
    IF(@PropertyCount = 0 )
    BEGIN
    SET @NumberOfDays = 30
    -- CHECK TO SEE IF THERE ARE LISTINGS IN THE LAST 30 DAYS IN THE SAME LOCATION
    SELECT  @PropertyCount = COUNT(*) from properties where location = @location 
            AND DATEDIFF(day,listingtime,GETDATE()) < @numberOFDays
            AND PropertyID != @propertyID
        IF(@PropertyCount = 0 )
        BEGIN
        -- CHECK TO SEE IF THERE ARE LISTINGS IN THE LAST 30 DAYS IN THE SAME CITY
        SELECT @PropertyCount = Count(*) from properties where city = @city 
                AND DATEDIFF(day,listingtime,GETDATE()) < @numberOFDays
                AND PropertyID != @propertyID
        END
        ELSE
        SET @IsLocation = 1 --There are properties in the same location in the last 30 days
    END
    ELSE
    SET @IsLocation  = 0 -- There are properties listed int he city in the last 10 days
End
Else
SET @IsLocation = 1
-- This is where the appropriate results are returned. 
IF(@IsLocation = 1)
Begin
SELECT * ,(SELECT AVG(PRICE) as AveragePrice
       FROM PROPERTIES 
       WHERE DATEDIFF(day,listingtime,GETDATE()) < @numberOFDays
         AND Location = @Location
         AND PropertyID != @propertyID)
FROM Properties 
WHERE DATEDIFF(day,listingtime,GETDATE()) < @numberOFDays
      AND Location = @Location
      AND PropertyID != @propertyID
End
ElSE
SELECT * ,(SELECT AVG(PRICE) as AveragePrice
      FROM PROPERTIES 
          WHERE DATEDIFF(day,listingtime,GETDATE()) < @numberOFDays
          AND City = @City
          AND PropertyID != @propertyID)
FROM Properties 
WHERE DATEDIFF(day,listingtime,GETDATE()) < @numberOFDays
      AND City = @City 
      AND PropertyID != @propertyID
End
Run Code Online (Sandbox Code Playgroud)

您可能需要更改位置和城市外键的一些数据类型,因为我将它们用作 varchar。