Solved

SQL Server ROW_NUMBER Query

Posted on 2013-11-20
5
562 Views
Last Modified: 2013-11-20
Hi,
I use the ROW_NUMBER function fairly regularly and can normally get it to do what I'm achieving. However, I'm getting some odd behaviour which I can't figure out. I'm running the following query:

	
	SELECT 
		LearnerRef, 
		ROW_NUMBER() OVER(PARTITION BY LearnerRef ORDER BY LearnerRef ASC, GuidedLearnHours DESC),
		AimRef, 
		GuidedLearnHours
		--INTO #TempStudentGLHTable
	FROM 
		LearnerDataAimsTable
	WHERE 
		AimRef NOT LIKE 'CMISC%' AND
		ActualEndDate IS NULL
	ORDER BY
		LearnerDataAimsTable.LearnerRef ASC,
		LearnerDataAimsTable.GuidedLearnHours DESC

Open in new window


I'd expect this to rank the GuidedLearnHours for each learner from high to low, but instead I get this:

LearnerRef      (No column name)      AimRef      GuidedLearnHours
10                           1                              50096370      41  
10                           2                              50067552      328  
10                           3                              50022660      164  
10                           4                              50025946      164  

I've tried various combinations of ORDER BY, both within the function and the query and neither seems to achieve what I want (my understanding is that the ROW_NUMBER function effectively runs as a sub-query anyway so it overrides the main query ranking for the purpose of assigning a row number).
0
Comment
Question by:Karl_mark
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39662521
>ORDER BY LearnerRef ASC
>I'd expect this to rank ... from high to low
Change ASC to DESC in your T-SQL
0
 
LVL 9

Expert Comment

by:COANetwork
ID: 39662538
Remove the ASC order by LearnerRef from inside the function
SELECT 
		LearnerRef, 
		ROW_NUMBER() OVER(PARTITION BY LearnerRef ORDER BY GuidedLearnHours DESC),
		AimRef, 
		GuidedLearnHours
		--INTO #TempStudentGLHTable
	FROM 
		LearnerDataAimsTable
	WHERE 
		AimRef NOT LIKE 'CMISC%' AND
		ActualEndDate IS NULL
	ORDER BY
		LearnerDataAimsTable.LearnerRef ASC,
		LearnerDataAimsTable.GuidedLearnHours DESC

Open in new window

0
 

Accepted Solution

by:
Karl_mark earned 0 total points
ID: 39662540
Hi Jim,
Sorry, I figured it out. Stupidly, the source table has the GuidedLearnHours as char(5). I converted it to INT in the ORDER BY Clause within the ROW_NUMBER function and it now works.
It's a legacy table so don't blame me!
0
 

Assisted Solution

by:Karl_mark
Karl_mark earned 0 total points
ID: 39662541
So, the working code is:

	
        SELECT 
		LearnerRef, 
		ROW_NUMBER() OVER(PARTITION BY LearnerRef ORDER BY CAST(GuidedLearnHours AS INT) DESC),
		AimRef, 
		GuidedLearnHours
		--INTO #TempStudentGLHTable
	FROM 
		LearnerDataAimsTable
	WHERE 
		AimRef NOT LIKE 'CMISC%' AND
		ActualEndDate IS NULL
	ORDER BY
		LearnerDataAimsTable.LearnerRef ASC,
		LearnerDataAimsTable.GuidedLearnHours DESC

Open in new window

0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question