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 項目にも含まれていないことです。

You could order by Products.id. If rowid is going to be the same for each one you could order by max(rowid) or min(rowid) or add rowid to the group by statement.

于 2012-07-14T18:00:13.683 に答える
1

Are you trying to find the ID of the most recently inserted row? You want

 SELECT Scope_Identity()

Edit *I am trying to get the max row id of ROW_NUMBER()*

Wrap your query in

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

Alternately, a SELECT COUNT... query may provide the answer

于 2012-07-14T18:02:04.057 に答える