Friday, March 27, 2015

Generate 1 and 2 numbers alternatively using t-sql

Hi,


I have used the window functioning "NTILE()". This way the ID will be divided in 2 parts. The ordering is only not as mentioned in your example.


Maybe you can try this:



CREATE TABLE #TMP
(
ID SMALLINT,
NAME VARCHAR(20)
);

INSERT INTO #TMP(ID,NAME)
VALUES
(1,'Sam'),
(1,'John'),
(1,'Greg'),
(1,'Smith'),
(1,'Aron'),
(2,'Brad'),
(2,'Kevin'),
(2,'Alex'),
(2,'Jeff');

SELECT ID
,NAME
,NTILE(2) OVER(PARTITION BY ID ORDER BY ID) AS RN
FROM #TMP
ORDER BY ID

Regards,


Reshma




Please Vote as Helpful if an answer is helpful and/or Please mark Proposed as Answer or Mark As Answer when question is answered


No comments:

Post a Comment