使用Group by和Order by的存储过程

时间:2011-03-03 12:51:37

标签: sql tsql stored-procedures

我在存储过程中有这个。我希望按Offer_Continent分组记录,按Offer_CountryCode ASC分组,但不会按大陆顺序将结果分组。

以下是我的选择陈述

    SELECT
        Offer_SubCatName,
        Offer_TagLine,
        Offer_Type,         
        CustomOffersAttributes.Attribute_Value3,
        Offer_ID,
        Offer_ShortDescription,
        Offer_AccommRating,
        Offer_ResortName,
        Offer_AccommName,
        Offer_CountryCode,
        Offer_Continent,
        Offer_FlightSupplierCode,
        Offer_OriginAirport,
        Offer_DestinationAirport,
        CustomOffersAttributes.Attribute_Value4,
        CustomOffersAttributes.Attribute_Value,
        CustomOffersAttributes.Attribute_Value2,
        Op_Name                     
    FROM
        CustomOffers
        INNER JOIN CustomOffersAttributes ON CustomOffersAttributes.Attribute_ID = dbo.fn_CustomOffers_CheapestDatePrice(CustomOffers.Offer_ID)
        FULL OUTER JOIN Suppliers ON Suppliers.Op_Code = CustomOffers.Offer_FlightSupplierCode
    WHERE
        (
            @CategoryID = 0
        OR
            Offer_CatID = @CategoryID
        )
        AND
        (
            @OfferIDs = ''
            OR
            Offer_ID in (select val from fn_CreateInList(@OfferIDs))
        )
        AND
        (
            @SubCategoryName = ''
        OR
            Offer_SubCatName = @SubCategoryName
        )
        AND ISNULL(Offer_Starts,GETDATE()) <= GETDATE()
        AND ISNULL(Offer_Expires,GETDATE()) >= GETDATE()

    GROUP BY
        Offer_Continent,
        Offer_SubCatName,
        Offer_TagLine,
        Offer_Type,         
        CustomOffersAttributes.Attribute_Value3,            
        Offer_ID,
        Offer_ShortDescription,
        Offer_AccommRating,
        Offer_ResortName,
        Offer_AccommName,
        Offer_CountryCode,
        Offer_Continent,
        Offer_FlightSupplierCode,
        Offer_OriginAirport,
        Offer_DestinationAirport,
        CustomOffersAttributes.Attribute_Value4,
        CustomOffersAttributes.Attribute_Value,
        CustomOffersAttributes.Attribute_Value2,
        Op_Name 
    ORDER BY 
         Offer_Continent ASC, 
         Offer_CountryCode ASC

上面的结果如下所示

非洲(Offer_Continent)

  • 肯尼亚 - (Offer_CountrCode)

加勒比(Offer_Continent)

  • 安提瓜 - (Offer_CountrCode)
  • 巴巴多斯 - (Offer_CountrCode)
  • 格林纳达 - (Offer_CountrCode)
  • 蒙特哥湾 - (Offer_CountrCode)
  • 金士顿 - (Offer_CountrCode)
  • St Lucia - (Offer_CountrCode)
  • Tobago - (Offer_CountrCode)

但我想要的是Offer_CountryCode的唯一订单 但是,当我按Offer_CountryCode订购时,它看起来就像下面的

加勒比(Offer_Continent)

  • 安提瓜 - (Offer_CountrCode)
  • 巴巴多斯 - (Offer_CountrCode)
  • 格林纳达 - (Offer_CountrCode)
  • 蒙特哥湾 - (Offer_CountrCode)
  • 金士顿 - (Offer_CountrCode)

Afirca(Offer_Continent)

  • 肯尼亚 - (Offer_CountrCode)

加勒比(Offer_Continent)

  • St Lucia - (Offer_CountrCode)
  • Tobago - (Offer_CountrCode)

你可以看到加勒比海被重复了。这就是为什么我想使用GROUP BY Clause.Anyway我想要的是一个看起来像下面的recors集。

加勒比(Offer_Continent)

  • 安提瓜 - (Offer_CountrCode)
  • 巴巴多斯 - (Offer_CountrCode)
  • 格林纳达 - (Offer_CountrCode)
  • 蒙特哥湾 - (Offer_CountrCode)
  • 金士顿 - (Offer_CountrCode)
  • St Lucia - (Offer_CountrCode)
  • Tobago - (Offer_CountrCode)

非洲(Offer_Continent)

  • 肯尼亚 - (Offer_CountrCode)

希望我很清楚我要做什么..?提前谢谢..

2 个答案:

答案 0 :(得分:2)

您不需要使用group by,因为您没有使用任何聚合函数。您可以将要使用的任何字段添加到order by中,而不是使用group by

答案 1 :(得分:1)

好的,所以你想按大洲和国家订购,但各大洲的顺序应该由每个大陆最早的国家决定。

我认为以下内容可行:

;WITH Offers AS (
SELECT
    Offer_SubCatName,
    Offer_TagLine,
    Offer_Type,         
    CustomOffersAttributes.Attribute_Value3,
    Offer_ID,
    Offer_ShortDescription,
    Offer_AccommRating,
    Offer_ResortName,
    Offer_AccommName,
    Offer_CountryCode,
    Offer_Continent,
    Offer_FlightSupplierCode,
    Offer_OriginAirport,
    Offer_DestinationAirport,
    CustomOffersAttributes.Attribute_Value4,
    CustomOffersAttributes.Attribute_Value,
    CustomOffersAttributes.Attribute_Value2,
    Op_Name                     
FROM
    CustomOffers
    INNER JOIN CustomOffersAttributes ON CustomOffersAttributes.Attribute_ID = dbo.fn_CustomOffers_CheapestDatePrice(CustomOffers.Offer_ID)
    FULL OUTER JOIN Suppliers ON Suppliers.Op_Code = CustomOffers.Offer_FlightSupplierCode
WHERE
    (
        @CategoryID = 0
    OR
        Offer_CatID = @CategoryID
    )
    AND
    (
        @OfferIDs = ''
        OR
        Offer_ID in (select val from fn_CreateInList(@OfferIDs))
    )
    AND
    (
        @SubCategoryName = ''
    OR
        Offer_SubCatName = @SubCategoryName
    )
    AND ISNULL(Offer_Starts,GETDATE()) <= GETDATE()
    AND ISNULL(Offer_Expires,GETDATE()) >= GETDATE()
)
SELECT
    *
FROM
    Offers o1
ORDER BY
    (SELECT MIN(Offer_Country) from Offers o2 where o2.Offer_Continent = o1.Offer_Continent) asc,
    Offer_Country