Solved

how to select every nth row

Posted on 2007-11-26
9
3,940 Views
Last Modified: 2008-04-30
how might I select every nth row of a larger query?
For instance, I have a query that returns 1000 records, but I want to display only the 1st, 5th, 10, etc
0
Comment
Question by:rmirabelle
9 Comments
 
LVL 17

Expert Comment

by:HuyBD
ID: 20354851
If your table have a PK, try this

HuyBD
select * from (select (select count(*) from yourtable where id<t.id) as no,t.* from yourtable as t) as M where no in(1,5,10)

Open in new window

0
 
LVL 7

Expert Comment

by:macentrap
ID: 20354929
If you have autoincrementing ID field Try this:

SELECT * FROM Items WHERE ID = '1' or ID % 5 = 0
Order by ID Asc

0
 
LVL 25

Expert Comment

by:imitchie
ID: 20354930
while huyBD is correct, doing that count so many times is a bit inefficient

SELECT @rownum:=@rownum+1 rownum, t.*
FROM (SELECT @rownum:=0) r, mytable t

WHERE rownum %5=0
 and <other conditions>
ORDER BY ....

where T is your original table
%5=0 means every row that is a multiple of 5
%6=0 means every row that is a multiple of 6
etc
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 25

Expert Comment

by:imitchie
ID: 20354940
macentrap - wouldn't work for deleted rows....
0
 
LVL 7

Expert Comment

by:macentrap
ID: 20355976
if its auto-incrementing than it wont matter about deleted rows of ID field
0
 
LVL 25

Expert Comment

by:imitchie
ID: 20356058
i'm going to assume that
every nth row... 1st, 5th, 10, is a mistype, that it should actually be
5th, 10th, 15th etc.
i.e. that the questioner is after a sampling of data skipping rows, not by id but really by number of rows in the "would-be result without skipping"

SELECT * FROM Items WHERE ID = '1' or ID % 5 = 0
Order by ID Asc

if my remaining records are IDs 2,4,6,8,11,23,26,28,29,31,35
your return result based on ID will be... nothing?
0
 
LVL 7

Expert Comment

by:macentrap
ID: 20356111
I know - thats why my suggestion was made based on autoincrementing ID instead of counted records
0
 
LVL 1

Author Comment

by:rmirabelle
ID: 20356323
I think I'm following the suggestions, but am getting confused about how to include the subquery in the context of a more complex sql statement:

SELECT somefield, thedate FROM t1 LEFT OUTER JOIN t2 ON (t1.fk = t2.pk) WHERE t1.anotherfield NOT NULL GROUP BY thedate HAVING somefield > 0

any way to mix the statement in here with the left outer join and the having clause?

Thanks in advance
0
 
LVL 25

Accepted Solution

by:
imitchie earned 500 total points
ID: 20356445
here's an example using some joomla tables:
the inner query sets up the row numbers, the outer query in this case causes rows 1,5,9,13.. to be selected

SELECT *
FROM (
SELECT @row := @row +1 AS rownum, sum( catid ) AS somefield, date( created )
FROM (SELECT @row :=0) r, lf_content t1
LEFT OUTER JOIN lf_categories t2 ON ( t1.catid = t2.id )
WHERE t1.id IS NOT NULL
GROUP BY date( created )
HAVING somefield >0
)ranked
WHERE rownum %4 =1
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

713 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