I have the below table which has 2 columns.I need to write a query which will return 3 columns,the first 2 column should match the 2 columns present in the table and the 3rd column should group into n splits.
for example in this case I want to split into 4 groups.so 1st 4 rows should return 1 in column 3,2nd 4 rows should return 2, 3rd 4 rows should return 3 and finally last 3 rows(as there are in total 15 rows) should return 4.
DECLARE @NAMES TABLE ( [ID] INT IDENTITY, [NAME] VARCHAR(20) )
INSERT INTO @NAMES SELECT 'NAME1'
UNION ALL
SELECT 'NAME2'
UNION ALL
SELECT 'NAME3'
UNION ALL
SELECT 'NAME4'
UNION ALL
SELECT 'NAME5'
UNION ALL
SELECT 'NAME6'
UNION ALL
SELECT 'NAME7'
UNION ALL
SELECT 'NAME8'
UNION ALL
SELECT 'NAME9'
UNION ALL
SELECT 'NAME10'
UNION ALL
SELECT 'NAME11'
UNION ALL
SELECT 'NAME12'
UNION ALL
SELECT 'NAME13'
UNION ALL
SELECT 'NAME14'
UNION ALL
SELECT 'NAME15'
try NTILE() function in SQL Server