2012-07-14 47 views
0

我需要在將值插入到變量後對查詢結果進行排序。 我想按照'RowId'進行排序,但在我的情況下無效。
以下是我的查詢,我該如何使它工作? 謝謝。SQL Server,將值插入變量並排序

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 

回答

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) 

我一定要考,但我THIK都將工作。


問題是rowid不在任何組中。您可以訂購Products.id。如果rowid將與每個訂單相同,您可以通過max(rowid)min(rowid)訂購或將rowid添加到羣組聲明中。

+0

對不起,我忘了添加導致所有問題的@NumOfProducts。 – Eyal 2012-07-14 18:04:43

+0

這是無效的SQL,您無法將其分配給變量併爲其設置別名。但是我認爲你可以在'ORDER BY'之後放置'ROW_NUMBER()OVER(ORDER BY Products.Id)'' – Hogan 2012-07-14 18:07:09

+0

非常感謝! – Eyal 2012-07-14 18:16:57

1

您是否在嘗試查找最近插入的行的ID?你想

SELECT Scope_Identity() 

編輯 *我試圖讓ROW_NUMBER的最大行ID()*

環繞你的查詢中

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

或者,SELECT COUNT ...查詢可能會提供答案

+0

對不起,我忘了添加導致所有問題的@NumOfProducts。 – Eyal 2012-07-14 18:04:03

+0

不,我想獲取ROW_NUMBER()的最大行ID。問題是當我嘗試將值傳入@NumOfProducts時,我無法排序。 – Eyal 2012-07-14 18:07:08