dev_huesca
dev_huesca

Reputation: 506

How to group columns values and rank them in tsql

Spend half an hour trying to format my question but failed terribly (WHY IS IT SO DAMN HARD!?), so instead I'm going to post a picture that explains everything quite simply.
enter image description here

Some explanation, I have a table that has a set of columns that repeat in a predictable pattern, I want to query that pattern (please look at the picture to understand).

I'm still an amateur with SQL Server (started less than a month ago) and don't know where to begin.

Upvotes: 2

Views: 104

Answers (1)

Lukasz Szozda
Lukasz Szozda

Reputation: 175576

Use DENSE_RANK:

SqlFiddle

SELECT 
   row_number,
   [GROUP_NUMBER] = DENSE_RANK() OVER (ORDER BY column1, column2, column3),
   column1,
   column2,
   column3,
   random_value
FROM tab
ORDER BY Row_Number;

Upvotes: 3

Related Questions