x
login about faq Site discussion (meta-askssc)

How to return the searched record along with five next records

If I want to return six values from the middle of the table after passing the where clause and it should return the specified value along with the five next record. As for example: select * from employee where id='E000' then I want to return the record of E000 as well as the next five records of E001,E002,E003,E004,E005. Please help me to do this.

more ▼

asked Apr 16 '12 at 03:11 AM in Default

imteyazkhan374 gravatar image

imteyazkhan374
10 1 2 2

(comments are locked)
10|1200 characters needed characters left

3 answers: sort oldest

/check this it might help/

use AdventureWorks2008R2;

declare @i int;
declare @p int;
declare @q int;
declare @RTable table (id int, name varchar(100));

set @i = (SELECT [ContactTypeID] FROM [AdventureWorks].[Person].[ContactType] where [ContactTypeID] = 5);
set @p = @i;
set @q = @i+5;

while (@p <= @q)
begin
    insert into @RTable
    select [ContactTypeID],Name FROM [AdventureWorks].[Person].[ContactType] where [ContactTypeID] = @p
    set @p = @p + 1;
    if (@p > @q)
       break;
end
select * from @RTable   
more ▼

answered Apr 16 '12 at 05:41 AM

ykrao7 gravatar image

ykrao7
0

That may or may not return the correct records. But it's not how a database should be used. The database already contains the set that the six rows make up. Deconstructing them row by row, and reconstructing them into a table (row by row) is far from the best way to use a database.

Apr 16 '12 at 06:26 AM Magnus Ahlkvist
(comments are locked)
10|1200 characters needed characters left

Perhaps like this?

SELECT TOP 6 * FROM Employee WHERE id >= 'E000' ORDER BY id
more ▼

answered Apr 16 '12 at 06:22 AM

Magnus Ahlkvist gravatar image

Magnus Ahlkvist
13.7k 13 17 30

(comments are locked)
10|1200 characters needed characters left

Personally, I do not like such alphanumeric incremented columns. You get more pains than gains unless handled as computed columns etc. In your case, if you would follow Magnus Ahlkvist's very good suggestion (which actually gives the core of the solution i.e. TOP and ORDER BY), then 'E10001' would always be sorted before 'E1001' which would not be correct. So it would always be dependent upon the LOGIC and constraints of your such alphanumeric computation of the column. If I assume that there is one alphabet and then multiple numeric characters, then one way of doing it could be

SELECT TOP 6 * FROM Employee WHERE id >= 'E000' 
ORDER BY CAST(RIGHT(id, LEN(id)-1) AS BIGINT)

But again, that could break anytime if the LOGIC of computation of the column is changed or not the same.

more ▼

answered Apr 16 '12 at 06:45 AM

Usman Butt gravatar image

Usman Butt
13.8k 6 8 14

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments



Facebook logo Follow Ask SSC on Facebook
Find Ask SSC on Google+
linkedin logo Find us on LinkedIn

Topics:

x1843

asked: Apr 16 '12 at 03:11 AM

Seen: 362 times

Last Updated: Apr 16 '12 at 07:26 AM

Copyright © 2002-2012 Simple Talk Publishing. All Rights Reserved. If you have any queries, please contact the site administrators.
Ask SQL Server Central is a community service provided by Red Gate.