sql-使用聚合函数(最小/最大)作为select语句的一部分

sql-使用聚合函数(最小/最大)作为select语句的一部分,sql,aggregate-functions,Sql,Aggregate Functions,我试图返回别墅预订系统的最低和最高价格。我有一个查找表,其中存储了每栋别墅每周的价格 我在select中使用min和max函数来实现这一点,但我遇到了很多问题。谁能解释一下我哪里出错了?这是sp ALTER PROCEDURE spVillaGet -- Add the parameters for the stored procedure here @accomodationTypeFK int = null, @regionFK int = null, @arrivalDate datet

我试图返回别墅预订系统的最低和最高价格。我有一个查找表,其中存储了每栋别墅每周的价格

我在select中使用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
作为 开始 -已添加SET NOCOUNT以防止额外的结果集丢失 -干扰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
您没有详细说明遇到了什么问题,但这可能是一个问题:您需要指定GROUPBY子句中的所有非聚合列,即:

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)
谢谢你的帮助

当我按分组并包含select中除两个函数之外的所有列时,我得到以下错误

Msg 306, Level 16, State 2, Procedure spVillaGet, Line 22
文本、ntext和图像数据类型不能进行比较或排序,除非使用IS NULL或LIKE运算符。 Msg 306,16级,状态2,程序spVillaGet,第22行
文本、ntext和图像数据类型无法进行比较或排序,除非使用IS NULL或LIKE运算符。

需要更多有关您遇到的错误的详细信息问题和错误是什么?我已更新了有关此问题的答案。注意:你不应该提交答案来澄清你的问题-你应该更新问题和/或对我的答案发表评论。他还没有代表发表评论,但你是对的,改变他的问题会更好。