2008-11-26 75 views
1

我試圖返回別墅預訂系統的最低和最高價格。我有一張查詢表,用於存儲每個別墅每週的價格。sql - 使用聚合函數(最小/最大)作爲select語句的一部分

我使用最小和最大函數來做到這一點內選擇,但我有很多問題。任何人都可以解釋我要去哪裏錯了嗎?繼承人的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語句的干擾。 SET NOCOUNT ON;

-- 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 
+0

需要更多詳細信息,你正在得到什麼錯誤 – Greg 2008-11-26 10:37:44

+0

有什麼問題,你面臨的錯誤? – Dheer 2008-11-26 10:39:21

回答

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) 
0

感謝您的幫助

當我GROUP BY和包括所有從選擇,除了這兩個功能我收到以下錯誤

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

文本列, ntext和圖像數據類型不能進行比較或排序,除非使用IS NULL或LIKE運算符。 消息306,級別16,狀態2,過程spVillaGet,行22 除使用IS NULL或LIKE運算符時,不能比較或排序文本,ntext和圖像數據類型。

相關問題