我在存储过程中有这个。我希望按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_Continent)
但我想要的是Offer_CountryCode
的唯一订单
但是,当我按Offer_CountryCode
订购时,它看起来就像下面的
加勒比(Offer_Continent)
Afirca(Offer_Continent)
加勒比(Offer_Continent)
你可以看到加勒比海被重复了。这就是为什么我想使用GROUP BY Clause.Anyway我想要的是一个看起来像下面的recors集。
加勒比(Offer_Continent)
非洲(Offer_Continent)
希望我很清楚我要做什么..?提前谢谢..
答案 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