Solved

Getting every other record of a table.

Posted on 2006-11-28
3
485 Views
Last Modified: 2008-03-06
I want to have two queries written in standard sql (no "declare", no cursors etc.).

The first query has to return record no 1, 3, 5, 7 ... etc... - of a table containing names that will have to be sorted alphabetically.
The second has to return record 2, 4, 6, 8 ... etc... - of the same table.

There is no incrementally numbered id.

The result should look something like this:

result query1        result query 2

        aa                     ab
        ba                     bb
        ca                     cb
        cc                     cd

etc...
The query has to be written in standard SQL because it is supposed to be used in a gridview in MSSQL 2005 without using stored procedures...

Can it be done?

Rune
0
Comment
Question by:RunePerstrup
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 18028452
>it is supposed to be used in a gridview in MSSQL 2005 without using stored procedures...
why without a stored procedure? a gridview should be able to be fed from a stored procedure?
or what am I missing...

anyhow:

select * from (
select t.*, row_number() over (order by somefield ) r from yourtable t
) as l
where r % 2 = 1

and


select * from (
select t.*, row_number() over (order by somefield ) r from yourtable t
) as l
where r % 2 = 0
0
 

Author Comment

by:RunePerstrup
ID: 18028510
Because I only get Lots of trouble when i try use input and output variables. I just can't get it to work - and i have a deadline on this little thing, so it's really nice to avoid the problem for now.

Thx for your fast response. It is very appreciated. I didn't know of the the row_number() function ;-)

Great!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18029145
>I didn't know of the the row_number() function ;-)
it's one of the nice little new things in sql server 2005
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question