我正在显示下表中的属性。现在我要做的是,在相同的位置找到特性(假设我的特性位于 sec-19,匹配 sec-19,如果没有找到,则搜索整个城市),条件如下它应该在 10 天前发布,或者如果没有在 10 天前发布,则根据 30 天前的结果。
我有下面提到的下表(属性):
现在我要从此表中检索的是上市时间少于 10 天的那些特性的 'Propertyid' 和 'Average Price' 如果没有一个少于 10 天则返回少于 30 天的结果天。
谁能帮我解决这个问题。提前致谢。
或者只是 Any body can answer me without the Location match.
我需要根据 10 天前发布的属性计算“平均价格”,如果 10 天前没有发布属性,则将其视为 30 天前。像这样:
Select AVG(Price) As Average_Price from Properties where (DATEDIFF(day,listingtime,getdate())<30 or DATEDIFF(day,listingtime,getdate())<10)
但这里我只得到一个字段“平均价格”,而且我也没有检查以过滤它是 10 天前还是 30 天前发布的。 Knidly 重新检查并尝试解决我的问题。提前致谢。
最佳答案
我在这方面花了一些时间,我相信我已经解决了您的所有疑虑。我不完全确定城市或位置的数据类型,所以我使用了 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
您可能需要更改位置和城市外键的某些数据类型,因为我将它们用作 varchars。
关于sql - 条件查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2235889/