1

我正在尝试返回别墅预订系统的最低和最高价格。我有一个查找表,存储每个别墅每周的价格。

我在选择中使用 min 和 max 函数来执行此操作,但我遇到了很多问题。谁能解释我哪里出错了?这里是sp

ALTER PROCEDURE spVillaGet 
-- Add the parameters for the stored procedure here
@accomodationTypeFK int = null,
@regionFK int = null,
@arrivalDate datetime = null,
@numberOfNights int = null,
@sleeps int = null,
@priceFloor money = null,
@priceCeil money = null

AS BEGIN -- 添加了 SET NOCOUNT ON 以防止额外的结果集 -- 干扰 SELECT 语句。设置无计数;

-- Insert statements for procedure here
SELECT tblVillas.name, 
       tblVillas.introduction,
       tblVillas.italian_introduction,
       tblVillas.uk_content,
       tblVillas.italian_content,
       tblVillas.sleeps,
       tblVillas.postcode,
       tblLkUpRegions.regionName,
       tblLkUpAccomodationTypes.accomodationType,
       MIN(price) As MinPrice,
       MAX(price) As MaxPrice

FROM tblVillas

LEFT JOIN tblLkUpRegions on tblVillas.regionFK = tblLkUpRegions.regionID
LEFT JOIN tblLkUpAccomodationTypes on tblVillas.accomodationTypeFK = tblLkUpAccomodationTypes.accomodationId    
LEFT JOIN tblWeeklyPrices on tblWeeklyPrices.villaFK = tblVillas.villaId

WHERE

    ((@accomodationTypeFK is null OR accomodationTypeFK = @accomodationTypeFK)
     AND (@regionFK is null OR regionFK = @regionFK)
     AND (@sleeps is null OR sleeps = @sleeps) 
     AND tblVillas.deleted = 0)

GROUP BY tblVillas.name
4

2 回答 2

3

您没有详细说明您遇到了什么问题,但这可能是一个问题:您需要在 GROUP BY 子句中指定所有非聚合列,即:

GROUP BY tblVillas.name, 
       tblVillas.introduction,
       tblVillas.italian_introduction,
       tblVillas.uk_content,
       tblVillas.italian_content,
       tblVillas.sleeps,
       tblVillas.postcode,
       tblLkUpRegions.regionName,
       tblLkUpAccomodationTypes.accomodationType

从您的后续评论中可以看出,您的某些列属于不能在 GROUP BY 子句中使用的数据类型。试试这个:

SELECT tblVillas.name, 
           tblVillas.introduction,
           tblVillas.italian_introduction,
           tblVillas.uk_content,
           tblVillas.italian_content,
           tblVillas.sleeps,
           tblVillas.postcode,
           tblLkUpRegions.regionName,
           tblLkUpAccomodationTypes.accomodationType,
           (SELECT MIN(price) FROM tblWeeklyPrices where tblWeeklyPrices.villaFK = tblVillas.villaId) As MinPrice,
           (SELECT MAX(price) FROM tblWeeklyPrices where tblWeeklyPrices.villaFK = tblVillas.villaId) As MaxPrice
FROM tblVillas
LEFT JOIN tblLkUpRegions on tblVillas.regionFK = tblLkUpRegions.regionID
LEFT JOIN tblLkUpAccomodationTypes on tblVillas.accomodationTypeFK = tblLkUpAccomodationTypes.accomodationId    
WHERE
        ((@accomodationTypeFK is null OR accomodationTypeFK = @accomodationTypeFK)
         AND (@regionFK is null OR regionFK = @regionFK)
         AND (@sleeps is null OR sleeps = @sleeps) 
         AND tblVillas.deleted = 0)
于 2008-11-26T10:34:30.443 回答
0

谢谢你的帮助

当我分组并包括选择中的所有列时,除了两个函数之外,我收到以下错误

Msg 306, Level 16, State 2, Procedure spVillaGet, Line 22

text、ntext 和 image 数据类型不能进行比较或排序,除非使用 IS NULL 或 LIKE 运算符。消息 306,级别 16,状态 2,过程 spVillaGet,第 22 行 无法比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符。

于 2008-11-26T10:47:58.123 回答