Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT rows that are a multiple of x

Tags:

sql

sql-server

In SQL Server, how do I select rows 10, 20, 30, 40, etc where the RowID is an equal gap of some integer (+10). There are 50k rows, so using IN (1,10,20,etc) is laborious.

SELECT * FROM 'TABLENAME' WHERE RowID = 10 (+ 10)
like image 328
Daniel Comp Avatar asked Jul 14 '13 02:07

Daniel Comp


People also ask

How do I SELECT a certain number of rows?

Select one or more rows and columnsSelect the row number to select the entire row. Or click on any cell in the row and then press Shift + Space. To select non-adjacent rows or columns, hold Ctrl and select the row or column numbers.

How do I SELECT multiple rows in SQL?

Syntax - SELECT column1,column2, …, columnN FROM table_name; column1,column2 – Specifies the name of the columns used to fetch. table_name - Specifies the name of the table.

Can we use SELECT * with group by?

Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause. The original idea was to create the table in beginning of the query, so the (SELECT * FROM #TBL) could be used on the query itself, instead of defining the names on each GROUP BY.


1 Answers

You can use modulo for that.

SELECT * FROM `table` WHERE (`id` % 10) = 0

SELECT * FROM `table` WHERE (`id` MOD 10) = 0

SELECT * FROM `table` WHERE !MOD(`id`, 10)

Anyone should do.

like image 172
Havenard Avatar answered Sep 19 '22 22:09

Havenard