Select alternate rows from SQL Server table

Vikrant

I am working with SQL Server 2008. I have a table which does not contain any unique columns; how to get alternate rows from it?

SQL Server table:

+-----+--------+
|  id |  name  |
|-----+--------|
|  1  |  abc   |
|  2  |  pqr   |
|  2  |  pqr   |
|  3  |  xyz   |
|  4  |  lmn   |
|  5  |  efg   |
|  5  |  efg   |
+-----+--------+

As we've to come with at least one working suggestion with the question, I've tried below code; which is not so proper technique when fetching from a huge amount of data.

Trial:

create table #tmp
(
    id int, name varchar(10), srNo int
)

insert into #tmp
   select 
      id, name,
      ROW_NUMBER() OVER (ORDER BY id) % 2 as srNo   --,alternate rows
   from 
      Employee

select * 
from #tmp 
where srNo = 1 --or srNo = 0

Above query gives out alternate rows i.e. 1st, 3rd, 5th OR 2nd, 4th, 6th etc.

Please help me out with proper way without #tmp to achieve the goal!

Thank you!

Bob

You can just use your select statement as an in-line view. You don't need the #tmp table.

select t.id, name
from (select id, name, ROW_NUMBER() over (order by id) as srNo from Employee) t
where (t.srNo % 2) = 1

SqlFiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL Server - How to SELECT values from different rows but in the same table

Select n random rows from SQL Server table

Select only rows with common status from table in SQL Server

Select percentage of rows from SQL table?

remove alternate rows from sql query Oracle

Insert rows into SQL Server table from json

SQL Server: select random rows with distinct id from table where id is not distinct

SQL Server : select all rows where Column does not contain any value from dynamic table of values

How to insert rows into a SQL table with a SELECT NOT EXIST statement from one server to another?

SQL - How to select discontinuous rows from a table with one select?

SQL: select rows from a certain table based on conditions in this and another table

Query table and Select latest 2 rows (in SQL Server)

How to make a table from a table of json rows in SQL Server

how to select alternate rows from mysql using php

SQL : select rows from table 1 and exists (rows from table 2)

SQL - how to select rows based upon values from a different table?

SQL SELECT repeating rows from table for specific time interval

SQL - Select rows from same table twice without using UNION

SQL Query select top and bottom rows from every batch in the table

SQL: How select rows and minus from another table

SQL: select rows from table where each element of a column is a matrix?

Select table from Sql server and insert data to Mysql table

SQL Server : select row if column value is not null from two rows

SQL Server - Get Data from Previous Rows in Select Statement

How to select data rows from sql server with the maximum value?

Select distinct duplicated rows from 1:n tables in SQL Server

Select Middle Rows in SQL Server

SQL Server query pulls two different rows from same table

Compare multiple rows from the same table in SQL Server 2005