Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Ranking Based On Value

Posted on 2016-11-10
3
Medium Priority
?
79 Views
Last Modified: 2016-11-13
I know how to do basic ranking, but is it possible to do it based on a sum of a value?

Using the small subset of data below, my goal is to sum up the "DescValue" column and every SUM of 5, I would increment the "Page" number. The "Page" must increment to never allow the sum of "DescValue" on a page to be greater than 5.

I am using MS SQL 2014.

Description            DescValue      Page
Line1                   1                  1
Line2                   2                  1
Line3                  1                  1
Line4                  2                  2
Line5                  3                  2
Line6                  2                  3
Line7                  4                  4
Line8                  1                  4
Line9                  1                  5
0
Comment
Question by:ScubeduFan
  • 2
3 Comments
 
LVL 35

Expert Comment

by:Pawan Kumar
ID: 41883326
ok got it , working. Good One Vitor :) and I totally take that back.

Regards,
Pawan
0
 
LVL 35

Accepted Solution

by:
Pawan Kumar earned 2000 total points
ID: 41883678
Here is the solution, try

Table creation

--

Create table five
(
	 Description varchar(100)           
	,DescValue  int  	
)
GO

INSERT INTO five VALUES
('Line1'            ,       1   ),     
('Line2'            ,       2     ),             
('Line3'            ,      1     ),             
('Line4'            ,      2     ),             
('Line5'             ,     3     ),             
('Line6'             ,     2     ),             
('Line7'            ,      4      ),            
('Line8'            ,      1     ),             
('Line9'           ,       1     )



--

Open in new window


--

;WITH CTE As
(
	SELECT * , ROW_NUMBER() OVER (ORDER BY (SELECT 1)) rnk FROM Five 
)
,CTE1 AS
(
	SELECT Description , DescValue a1 , DescValue , rnk , 1 Lvl FROM CTE WHERE rnk = 1
	UNION ALL
	SELECT c1.Description , c1.DescValue a1, 
	CASE WHEN c1.DescValue + c.DescValue > 5 THEN c1.DescValue ELSE c1.DescValue + c.DescValue END DescValue , c1.rnk 	
	,CASE WHEN c1.DescValue + c.DescValue > 5 THEN Lvl + 1 ELSE Lvl END Lvl
	FROM CTE c1 INNER JOIN CTE1 c ON c.rnk + 1 = c1.rnk
)
SELECT Description , DescValue , lvl Page FROM CTE1
OPTION (MAXRECURSION 0)

GO



--

Open in new window


Output
------------------
Description      DescValue      Page
Line1               1                       1
Line2               3                       1
Line3               4                       1
Line4               2                       2
Line5               5                       2
Line6               2                       3
Line7               4                       4
Line8               5                       4
Line9               1                       5



Hope it helps !!
0
 

Author Closing Comment

by:ScubeduFan
ID: 41885385
Works great! Thank You!!!
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

772 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