1

ヴィラ予約システムの最低価格と最高価格を返そうとしています。各ヴィラの毎週の価格を格納するルックアップ テーブルがあります。

選択内でこれを行うために最小関数と最大関数を使用していますが、多くの問題があります。誰が私が間違っているのか説明できますか? 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 ステートメントに干渉します。NOCOUNT をオンに設定します。

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

どのような問題が発生しているかについては詳しく説明していませんが、これはおそらく 1 つです。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

ご協力いただきありがとうございます

グループ化して、選択から2つの関数を除くすべての列を含めると、次のエラーが発生します

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

IS NULL または LIKE 演算子を使用する場合を除き、text、ntext、および image データ型を比較または並べ替えることはできません。メッセージ 306、レベル 16、状態 2、プロシージャ spVillaGet、行 22 IS NULL または LIKE 演算子を使用する場合を除き、text、ntext、および image データ型を比較または並べ替えることはできません。

于 2008-11-26T10:47:58.123 に答える