473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Stepping through Results

Hi all..

I am looking for some assistance on a query.

I have a Stored Procedure where I am attemtping to step through the results and return every other nth record..

Here is what I have so far:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM (SELECT TOP(' + @SampleAmount + ') * FROM (SELECT DISTINCT TOP(' + @TotalRecords + ') * FROM ' + @TableName + ' WHERE CountyNumber = ' + @CountyNumber + ' ORDER BY ID ASC) As Foo  ORDER BY ID DESC) As Bar  ORDER BY ID ASC'
@SampleAmount is the amount of records I want retruned
@TotalRecords is the total amount of records available
@TableName is duh, the table from which the recrods come
@CountyNumber is the county number to filter the results

This works fantastic for really large recordsets, like 10,000.. But i am seeing some inconsistancies when trying to step through a smaller amount.

Say There are only 700 TotalRecords availale, and I want 100 records returrned, with the above SP i woudl only get 7 records..If i wanted 40 records I would get 17 i think, and i think they exact cutoff where i would get exactly what i request is 26 records..

There is obviuously a flaw in my logic, can anyone assist in this??

Thanks in advance for any help!
Aug 9 '07 #1
3 1290
From what I can tell your code above will only return as many records as you specify in @SampleAmount

You could open a cursor and then have it insert every Nth record into a table variable, or temp table. Then return that as your recordset at the end of the procedure.

Example:

Expand|Select|Wrap|Line Numbers
  1. --Controlling Variables
  2. Declare @TableName varchar(30)
  3. Declare @CountyNumber int
  4. Declare @SampleAmount int
  5.  
  6. --Processing Variables 
  7. Declare @return_table table ([ID] int, [Data] varchar(25), [CountyNumber] int)
  8. Declare @ID int
  9. Declare @Data varchar(25)
  10. Declare @County int
  11. Declare @Counter int
  12. Declare myCursor scroll cursor for
  13. Select * 
  14. From ' + @TableName + '
  15. Where CountyNumber = ' + @CountyNumber + '
  16. Order By ID ASC
  17.  
  18. Set @Counter = 0
  19.  
  20. FETCH NEXT FROM myCursor into @ID, @Data, @County
  21.  
  22. While @@fetch_status = 0
  23. Begin
  24.  
  25. --Each time we load a record increment the counter by 1
  26. Set @Counter = @Counter + 1
  27.  
  28. --If we've reached our Nth record reset the counter, then insert the current record
  29. If @Counter = @SampleAmount
  30.     Begin
  31.     Set @Counter = 0
  32.     Insert into @return_table
  33.     (ID, Data, County)
  34.     Values
  35.     (@ID, @Data, @County)
  36.     End
  37.  
  38. FETCH NEXT FROM myCursor into @ID, @Data, @County
  39. End
  40.  
  41. Close myCursor
  42. Deallocate myCursor
  43.  
  44. --When were all done display out the results
  45. Select * From @return_table
Aug 10 '07 #2
THanks bwestover...

I was actually just coming to post that I had finally figured it out when I saw your reply. Thanks for the effort. It looks like our solutions are quite different.

Expand|Select|Wrap|Line Numbers
  1. 'SELECT *
  2. FROM
  3. (
  4.      SELECT *, RowNumber = ROW_NUMBER() OVER (PARTITION BY NTileNum ORDER BY ID ASC)
  5.      FROM
  6.      ( 
  7.           SELECT *, NTILE(' + @SampleAmount + ') OVER (ORDER BY ID) AS NTileNum
  8.           FROM ' + @TableName + '
  9.           WHERE CountyNumber = ' + @CountyNumber + '
  10.      ) As A
  11. ) As B
  12. WHERE RowNumber = 1'
  13.  
From what I can tell your code above will only return as many records as you specify in @SampleAmount

You could open a cursor and then have it insert every Nth record into a table variable, or temp table. Then return that as your recordset at the end of the procedure.

Example:

Expand|Select|Wrap|Line Numbers
  1. --Controlling Variables
  2. Declare @TableName varchar(30)
  3. Declare @CountyNumber int
  4. Declare @SampleAmount int
  5.  
  6. --Processing Variables 
  7. Declare @return_table table ([ID] int, [Data] varchar(25), [CountyNumber] int)
  8. Declare @ID int
  9. Declare @Data varchar(25)
  10. Declare @County int
  11. Declare @Counter int
  12. Declare myCursor scroll cursor for
  13. Select * 
  14. From ' + @TableName + '
  15. Where CountyNumber = ' + @CountyNumber + '
  16. Order By ID ASC
  17.  
  18. Set @Counter = 0
  19.  
  20. FETCH NEXT FROM myCursor into @ID, @Data, @County
  21.  
  22. While @@fetch_status = 0
  23. Begin
  24.  
  25. --Each time we load a record increment the counter by 1
  26. Set @Counter = @Counter + 1
  27.  
  28. --If we've reached our Nth record reset the counter, then insert the current record
  29. If @Counter = @SampleAmount
  30.     Begin
  31.     Set @Counter = 0
  32.     Insert into @return_table
  33.     (ID, Data, County)
  34.     Values
  35.     (@ID, @Data, @County)
  36.     End
  37.  
  38. FETCH NEXT FROM myCursor into @ID, @Data, @County
  39. End
  40.  
  41. Close myCursor
  42. Deallocate myCursor
  43.  
  44. --When were all done display out the results
  45. Select * From @return_table
Aug 10 '07 #3
Quite right. Very different indeed.

I can see you are using the new ranking features introduced in SQL 2005. Im still mostly coding in 2000 and Im not overly familiar with those functions. It looks much cleaner than having to declare a scrollable recordset.

Probably much faster as well.

Im glad you got it figured out.
Aug 10 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: c# newbie | last post by:
When stepping through code, to find where an error is thrown, the problem is that I have to step threw the statement that causes the error, and if it's in a class that's instantiated from the main...
0
by: Dean Slindee | last post by:
Anybody got this working or know of explicit instructions on how to get debug stepping thru a stored procedure with Visual Studio .NET and Windows 2000 OS? I get this error message "Unable to...
5
by: pinaki_m77 | last post by:
Hi, I am trying to debug a C++ program using Microsoft VC++ IDE. The program loads a dynamic link library (dll) and later makes calls to functions inside this dll. I want to step inside the code of...
25
by: David C | last post by:
I posted this question, and from the replies, I get the impression that I worded my posting very poorly, so let me try this again. While debugging and stepping through this foreach loop ...
0
by: stand__sure | last post by:
Stepping into a stored procedure used to be fairly straight-forward, but after following the guidance in all 6 or so of the MSDN pages about enabling debugging of stored procedures in SQL Server...
0
by: =?Utf-8?B?TWlrZSBPS0M=?= | last post by:
VB 2003 As I am stepping through code, I want to see a data table in a grid, so I can easily see the data in the data table. Currently I QuickWatch the data table, then I have to add ".rows(0)"...
7
by: Lyn | last post by:
Is there a solution to this problem? While stepping through code (F8) it would sometimes be helpful to observe changes occurring on the affected form in the Access window. However, the current...
1
by: lothar.behrens | last post by:
Hi, I have a base class that defines a MustOverride method. In a derived implementation class I am not able to set a break point nor stepping into by a call from the base class. What would...
1
by: BumbelingIntern | last post by:
Hi, I'm working on an applet that allows a user to step through the results of a query and edit the information. I have it so that you can click one button to get the image and another to update...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.