.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

ROW_NUMBER() OVER - PARTITION BY should be different from ORDER BY

Posted By:      Posted Date: August 28, 2010    Points: 0   Category :Sql Server
I have noticed incorrect usage for OVER - PARTITION BY - ORDER BY with same (BY) column lists, making the query nondeterministic as far as the generated ROW_NUMBER() is concerned.  If random sort needed, use NEWID() instead. Demo follows. -- WRONG PARTITION BY ... ORDER BY ... usage - nondeterministic sort SELECT ProductName = Name, Color, PartRowID = ROW_NUMBER() OVER(PARTITION BY Color ORDER BY Color) FROM AdventureWorks2008.Production.Product WHERE Color IS NOT NULL ORDER BY Color, PartRowID /* ProductName Color PartRowID .... Men's Bib-Shorts, L Multi 3 AWC Logo Cap Multi 4 Long-Sleeve Logo Jersey, S Multi 5 .... */ -- CORRECT PARTITION BY ... ORDER BY ... usage - UNIQUE sort (Name is unique) SELECT ProductName = Name, Color, PartRowID = ROW_NUMBER() OVER(PARTITION BY Color ORDER BY Name) FROM AdventureWorks2008.Production.Product WHERE Color IS NOT NULL ORDER BY Color, PartRowID /* ProductName Color PartRowID .... Long-Sleeve Logo Jersey, M Multi 3 Long-Sleeve Logo Jersey, S Multi 4 Long-Sleeve Logo Jersey, XL Multi 5 Men's Bib-Shorts, L Multi 6 .... */ Kalman Toth, SQL Server & Business Intelligence Training; SQL 2008 GRAND SLAM

View Complete Post

More Related Resource Links

replacing Group By with row_number(partition by ...)

I have an indexed view and I query this view to get counts on how many products each seller has in a specific category. This query works perfect and produces results in 45ms. I want to join additional tables and apply additional WHERE filters to the result set. I am not able to do it because I had to get products_pid out of With clause to be able to join other tables after CTE. I couldn't add products_pid to the select list because it would require me to have in the Group By. After reading articles in this forum, I found out that Group By can be replaced by row_number() over (partition by...). I re-wrote my query using row_number(). But this query takes 1100 ms to execute. Is this the write way to get rid of Group By? What am I doing wrong here and why it takes so long to execute?
Thank you,

Original query:
set statistics time on
with keys as (
select getproductssellers.productslocation_cid,
count(distinct getproductssellers.products_pid) as CNT
from GetProductsSellers WITH (NOEXPAND)
WHERE getproductssellers.products_subcategory=26
group by getproductssellers.productslocation_cid
select customer.businessname as Seller, productslocation_cid, CNT
from keys
inner join custom



Hi all

I have the following table:

ID IsTop Value
1 1 TestGroup1
2 0 Child 1 (TestGroup1)
3 0 Child 2 (TestGroup1)
4 1 TestGroup2
5 0 Child 1 (TestGroup2)
6 0 Child 2 (TestGroup2)
7 0 Child 3 (TestGroup2)

Now I need to find out the groups,
The ID 1, 2, 3 are one Group
and the IDs 4, 5, 6, 7 are another Group.
The group names are not in the value,
it is just for this sample to show what I mean.

To identify I need to get IsTop = 1 and all following
rows until IsTop again = 1.

I have tried to use it with PARTITION BY, but this does not work.
Any other ideas ?

Best regards
Frank Uray

P.S. Here are the script for testing:

CREATE TABLE #tempPARTITION ([ID] int, [IsTop] int, [Value] varchar(255))
INSERT INTO #tempPARTITION ([ID], [IsTop], [Value])
SELECT 1, 1, 'TestGroup1'
SELECT 2, 0, 'Child 1 (TestGroup1)'
SELECT 3, 0, 'Child 2 (TestGroup1)'
SELECT 4, 1, 'TestGroup2'
SELECT 5, 0, 'Child 1 (TestGroup2)'
SELECT 6, 0, 'Child 2 (TestGroup2)'
SELECT 7, 0, 'Child 3 (TestGroup2)'




Is it possible with SQL sorted in descending order according to the word?
In Table I shrajeno Mastercard, Visa, Diners etc ...

All Visa are the first in the order, another Mastercard, etc. ...

Something does not work: ORDER BY PaymentType = 'Visa';

I would not want to work in one field PaymentId (111,222,33, etc. ..) and then use the ORDER BY.

Is this possible in such a way?

Linq qry with order by basic questions


I have never tried a group by with linq, now I do and I'm stuck.

I try to convert this sql statement to its linq counterpart :

SELECT COUNT(tb_Mailer_Smses.Id) AS [cnt], tb_Mailer_mailing.Id As [Id]
,SUM( CASE WHEN tb_Mailer_smses.DLRStatus = 200 THEN 1 ELSE 0 END) As [Delivered]
FROM tb_Mailer_mailing INNER JOIN
tb_Mailer_Smses ON tb_Mailer_mailing.Id = tb_Mailer_Smses.MailingId
WHERE tb_Mailer_mailing.ApplicationId = 1
GROUP BY tb_Mailer_mailing.Id

Is there some one who can me show me the convertion process ?

ASP.NET: Collect Customer Order Information on an Internet Site Using XML and Web Forms


XML has quickly become the new data structure standard for everything from database access to cross-platform computing. XML is typically considered to be a vehicle for data exchange, dynamic data presentation, and data storage. However, the potential of XML far surpasses those limited applications. This article examines one new use: the gathering of data across a number of forms in an ASP.NET Beta 1 framework application. The sample program is a Web app used for ordering pizza. It uses ASP and C# to gather order information and then stores it in XML. To build the application, several concepts are explained, including data collection, order persistence using cookies, grouping input forms, and formatting the data for display.

Jeff Jorczak

MSDN Magazine September 2001

order by in combination with union


 Hi All,

How can I order this by count? so if it appears above and below the union statement, I want it on top

	SELECT distinct BIER.Naam, BIER_L2.L2_Naam, BIER_L3.L3_Naam from BIER_MATCH, BIER, BIER_L2, BIER_L3

	SELECT distinct BIER.Naam, BIER_L2.L2_Naam, BIER_L3.L3_Naam from BIER_MATCH, BIER, BIER_L2, BIER_L3
	where BIER_L2.L2_ID = BIER.L2_ID and BIER_L2.L3_ID = BIER_L3.L3_ID and RECEPT_ID = @RECEPT_ID 
	and BIER_MATCH.BIER_ID is null


Is it possible to filter KPI list in order to display only relevant KPIs


I  have created a KPI list and I want to show sections of the list on various parts of my site, however, I dont seem to be able to filter the list. Has anyone tried this and had any success?

Order by Nemeric column


Hi all,

Thank you for advance.

the Document Rank column is numeric and contain the values are 2,1,0,4,0,5,3,0,6

we need to get the document rank with the following orders

how can i do this?

http module calling order


Our site uses urlRewriter http module that does the url rewriting for us.

I want to create another http module that will handle our customize URL redirects. Now I want this new customize http module to be call before the UrlRewriter module (to avoid page not found messages the UrlRewriter generates). Is there a way we can contol the calling order ?

how to get current loop order number in "for each" ?

For Each item As pages.tbl1Row In pagesdim x as integerx = ?????response.write("current loop is :" & x )               Nextwhat code i need to replace with "?????" to get current loop order?

SSIS Changing Column Order during Transformation

First let me say, I really can't believe this chain of events myself--and they are happening to me. I am upgrading several DTS packages to SSIS on what will be my new production server.  These packages create tables, export them to a flat file, and ftp them off to other locations. What is happening (on the SSIS side) is that the OLE DB Source is reordering some of the columns on its own (moving them to the end of the table/file.  Then when my pickup/load routines run, the data is out of place and they fail. Can anyone please explain what is happening here with the mapping.  I have evaluated the table and the columns are in the order that I expect.  When I preview the source table in the OLE DB Source Editor the columns are in the correct order/alignment, but when them in the OLE DB Source Editor --Columns section within BIDS the order is changed arbitrarily. I have been somewhat successful (2 out of 3) in being able to re-map the data, but this last table just doesn't want to change.  Thanks in advance for any help and/or information you can provide

Pie Chart - possible to order the slices?

Is it possible to order the slices in a Reporting Services 2008 pie chart, so that they are in order of magnitude (size) from largest to smallest?   Thanks, Chris
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend