Solved

Getting every other record of a table.

Posted on 2006-11-28
3
473 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

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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 setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

867 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now