Last post Mar 16, 2017 05:21 PM by limno
Mar 16, 2017 07:35 AM|Shuklaji123|LINK
I want to ask one query, related to sql server 2016. I have one table, name and pwd, only. How can i get the alternate row, without using function or with using function ?Please help!!!!!!!!!!!!!!!!
Mar 16, 2017 07:47 AM|Chris Zhao|LINK
declare @tb table
(name varchar(20),pwd varchar(20))
insert into @tb values
;WITH CTE AS
SELECT ROW_NUMBER()OVER (ORDER BY name)AS ROW,* FROM @tb
SELECT name,pwd FROM CTE WHERE ROW%2=0
Mar 16, 2017 02:31 PM|limno|LINK
You may need to think about your table design as well.
What is your purpose of this requirement?
Why cannot yo use a function in your query?
Mar 16, 2017 04:00 PM|Shuklaji123|LINK
You can use a function for this, but you need to tell me how ? This was asked to me by interviewer.
Mar 16, 2017 05:21 PM|limno|LINK
There is a basic database concept in this question.
Rows in a table don't have a defined order in storage.
You need to define the order when you retrieve from table by using ORDER BY clause.
How do you define an alternate row?
It does not grantee the sequence. If you want to retrieve half of your rows,
you can define a sequence order with the row_number function and ORDER by newid() as an option.