Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate DENSE_RANK() without using order by clause - SQL Server

I am total new to SQL Server, can anyone guide me on my query it will very helpful for me.

I need to generate rank like in this example:

Report No               Rank 
----------------------------
18  3229                 1
89-0119-87               2
180261292                3
180261292                3
18  3107                 4
18A 7     A06            5
T18002415                6
T18002415                6
T18002415                6
0000000018-519           7
0000000018-519           7
0000000018-519           7
0000000018-519           7
0000000018-655           8
000000018-1002           9
000000018-1002           9
000000018-1002           9
000000018-5712           10

If I am generating rank using

DENSE_RANK() OVER(ORDER BY ReportNo DESC)

then I'm not getting the required output. Can you please guide me. How can I generate rank as in the above example?

like image 246
Parita Navadiya Avatar asked Jun 20 '26 17:06

Parita Navadiya


2 Answers

I could not see the logic of how Report No is ordered. As it's custom order, create anotherTable (temp table or table variable) containing required order Report No , Rank.

Report No           Rank 
18  3229                 1
89-0119-87               2
180261292                3
18  3107                 4
18A 7     A06            5
T18002415                6
0000000018-519           7
0000000018-655           8
000000018-1002           9
000000018-5712           10

Then

SELECT thisTable.ReportNo, anotherTable.[Rank]
FROM thisTable
INNER JOIN anotherTable ON thisTable.ReportNo = anotherTable.ReportNo
like image 173
qxg Avatar answered Jun 23 '26 15:06

qxg


Try this:

DECLARE @Tab TABLE(Report_No VARCHAR(100))

INSERT INTO @Tab VALUES('18  3229')
INSERT INTO @Tab VALUES('89-0119-87')
INSERT INTO @Tab VALUES('180261292')
INSERT INTO @Tab VALUES('180261292')
INSERT INTO @Tab VALUES('18  3107')
INSERT INTO @Tab VALUES('18A 7     A06')
INSERT INTO @Tab VALUES('T18002415')
INSERT INTO @Tab VALUES('T18002415')
INSERT INTO @Tab VALUES('T18002415')
INSERT INTO @Tab VALUES('0000000018-519')
INSERT INTO @Tab VALUES('0000000018-519')
INSERT INTO @Tab VALUES('0000000018-519')
INSERT INTO @Tab VALUES('0000000018-519')
INSERT INTO @Tab VALUES('0000000018-655')
INSERT INTO @Tab VALUES('000000018-1002')
INSERT INTO @Tab VALUES('000000018-1002')
INSERT INTO @Tab VALUES('000000018-1002')
INSERT INTO @Tab VALUES('000000018-5712')

SELECT *
    ,ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) RN
INTO #temp
FROM @Tab T1

SELECT T1.Report_No
    ,SUM(CASE WHEN T1.Report_No = T2.Report_No THEN 0 ELSE 1 END) OVER(ORDER BY T1.rn)[Rank]
FROM #temp T1
LEFT JOIN #temp T2 ON T1.rn = T2.rn+1

DROP TABLE #temp

Output:

Report_No       Rank
18  3229        1
89-0119-87      2
180261292       3
180261292       3
18  3107        4
18A 7     A06   5
T18002415       6
T18002415       6
T18002415       6
0000000018-519  7
0000000018-519  7
0000000018-519  7
0000000018-519  7
0000000018-655  8
000000018-1002  9
000000018-1002  9
000000018-1002  9
000000018-5712  10
like image 38
DineshDB Avatar answered Jun 23 '26 14:06

DineshDB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!