0

将值插入变量后,我需要对查询结果进行排序。我正在尝试根据“RowId”进行排序,但在我的情况下它无效。
以下是我的查询,我怎样才能使它工作?谢谢。

SELECT TOP 1 @NumOfProducts = ROW_NUMBER() OVER(ORDER BY Products.Id) AS RowId 
        FROM          Cities INNER JOIN
                      CitiesInLanguages ON Cities.Id = CitiesInLanguages.CityId INNER JOIN
                      ShopsInCities ON Cities.Id = ShopsInCities.CityId INNER JOIN
                      Categories INNER JOIN
                      ProductstInCategories ON Categories.Id = ProductstInCategories.CategoryId INNER JOIN
                      Products ON ProductstInCategories.ProductId = Products.Id INNER JOIN
                      ProductsInProdutGroup ON Products.Id = ProductsInProdutGroup.ProductId INNER JOIN
                      ProductsGroups ON ProductsInProdutGroup.ProductGroupId = ProductsGroups.Id INNER JOIN
                      ShopsInProductsGroup ON ProductsGroups.Id = ShopsInProductsGroup.ProductGroupId INNER JOIN
                      aspnet_Users ON ShopsInProductsGroup.ShopId = aspnet_Users.UserId ON ShopsInCities.ShopId = aspnet_Users.UserId INNER JOIN
                      ProductsNamesInLanguages ON Products.Id = ProductsNamesInLanguages.ProductId INNER JOIN
                      UsersInfo ON aspnet_Users.UserId = UsersInfo.UserId INNER JOIN
                      ProductOptions ON Products.Id = ProductOptions.ProductId INNER JOIN
                      ProductOptionsInLanguages ON ProductOptions.Id = ProductOptionsInLanguages.ProductOptionId INNER JOIN
                      ProductFiles ON Products.Id = ProductFiles.ProductId INNER JOIN
                      ProductsInOccasions ON Products.Id = ProductsInOccasions.ProductId INNER JOIN
                      Occasions ON ProductsInOccasions.OccasionId = Occasions.Id INNER JOIN
                      OccasionsInLanguages ON Occasions.Id = OccasionsInLanguages.OccasionId
WHERE     (Products.IsAddition = 0) AND (Categories.IsEnable = 1) AND (Products.IsEnable = 1) AND (ProductsGroups.IsEnable = 1) AND (Cities.IsEnable = 1) AND 
                      (ShopsInProductsGroup.IsEnable = 1) AND (CitiesInLanguages.CityName = @CityName) AND (ProductsNamesInLanguages.LanguageId = @languageId) AND 
                      (Categories.Id = @CategoryId) AND (ProductOptions.IsEnable = 1) AND (ProductFiles.IsEnable = 1)
                      group by Products.Id, ProductsNamesInLanguages.ProductName, UsersInfo.Name
                      Order By RowId
4

2 回答 2

1

用编辑试试这个:

SELECT TOP 1 @NumOfProducts = ROW_NUMBER() OVER(ORDER BY Products.Id),
                              ROW_NUMBER() OVER(ORDER BY Products.Id) AS RowId 

或尝试

ORDER BY ROW_NUMBER() OVER(ORDER BY Products.Id)

我必须进行测试,但我认为两者都可以。


问题是 rowid 不在任何 group by 项目中。

您可以按Products.id. 如果rowid每个您可以订购或添加到 group by 语句中的每个max(rowid)min(rowid)相同rowid

于 2012-07-14T18:00:13.683 回答
1

您是否要查找最近插入的行的 ID?你要

 SELECT Scope_Identity()

编辑 *我正在尝试获取 ROW_NUMBER()* 的最大行 ID

将您的查询包装在

 SELECT  @NumOfProducts = Max(RowID) FROM
 ( [your query here] ) v

或者,一个SELECT COUNT... 查询可以提供答案

于 2012-07-14T18:02:04.057 回答