SQL: Skip entries in an order without knowing total entry amount - mysql

The title is a bit confusing, but I'm wondering if there is a way to do a query like this:
SELECT * FROM table ORDER BY timestamp LIMIT 10
and then only take the ones after the 10th one (or none if there are less than or equal to 10 entries).
EDIT I guess another way to do this would be to order them by timestamp, descending, and then somehow limit to 0, (total-someNumber).

By specifying an OFFSET you can get the rows after a specified number. You combine this with limit.
In MySQL you achieve this with LIMIT [offset], limit.
Example - get 10 records after the oldest 10 records:
SELECT * FROM table ORDER BY timestamp LIMIT 10, 10; # Retrieve rows 11-20
Example - get 20 records after the newest 5 records:
SELECT * FROM table ORDER BY timestamp DESC LIMIT 5, 20; # Retrieve rows 6-25
If you want to get ALL rows after a certain number (eg. 10) then you pass an arbitrarily big number for the limit since it is required by the clause:
SELECT * FROM table ORDER BY timestamp LIMIT 10,18446744073709551615; # Retrieve rows 11-BIGINT
Note: 18446744073709551615 is the maximum of an unsigned BIGINT and is provided as the solution within the MySQL documentation.
See:
http://dev.mysql.com/doc/refman/5.5/en/select.html

I'd try something like this and then just add a where clause that skips the first n (n=10 in this case) rows.
i.e. using the linked example:
SELECT
*
FROM
(select #n := #n + 1 RowNumber, t.* from (select #n:=0) initvars, tbl t)
WHERE
RowNumber > 10

Related

get MAX date using limit and offset

Is there a way to use limit offset and get the most recent (MAX) date from that group
My table: column_id, column_data, column_date
I've tried
SELECT max(column_date) FROM table_name limit 2000 offset 22000
I'm trying to get the most recent date in the 2000 rows returned using the offset. In other words, I'm looking for the last date modified in each group of 2000.
The table structure above has 100,000 rows. each query gets 2000 rows and I would like to retrieve the most recent date from the 2000 rows (using offset).
You must extract the whole group then find MAX() over it:
SELECT MAX(date_column)
FROM ( SELECT date_column
FROM source_table
ORDER BY some_expression /* compulsory! must provide rows uniqueness! */
LIMIT #rows_in_group OFFSET #group_offset ) AS subquery

SQL: get only sampled data from large dataset

So I get a large amount of data from server using this SQL:
SELECT value,DATE_FORMAT(`time`,'%Y-%m-%dT%H:%i:%sZ') AS `time`
FROM history WHERE :id=reference AND
(time BETWEEN :start AND :end) ORDER BY time LIMIT 100 ";
Limit is set to fixed 100 entries.
But in given time range there could be 5 000 entries.
Here's my goal: I want to sample these entries by time between each entry.
So for example this interval between each entry will be 60 seconds (let's say it is parameter), then I will receive 100 entries (from 5000), but there will be always one minute difference between each one of them.
E.g.
value1,14:40:40
value2,14:41:40
...
value100,16:20:40
Is this doable via SQL? Or do I have to parse through this large data with PHP?
If it is not doable just with SQL, is it possible to get this 100 entries equally spread across this 5000 entries? (so not by time, but I'd get fixed entry id1,id50,id100,id150,...,id5000). Again just with sql.
Thanks!
Just as Kristof sais in his answer: Order the rows and take each nth row by applying a row number. This is how it is done in MySQL:
select
rows.value,
date_format(rows.`time`,'%Y-%m-%dT%H:%i:%sZ') AS `time`
from
(
select
#row_number := #row_number + 1 as row_number,
history.*
from history
cross join (select #row_number := 0) as t
where reference = :id and `time` between :start and :end
order by `time`
) as rows
cross join
(
select count(*) as cnt
from history
where reference = :id and `time` between :start and :end
) as rowcount
where mod(rows.row_number - 1, ceil(rowcount.cnt / 100)) = 0;
And this is how the same would look in another dbms, Oracle for instance, using analytic functions:
select
rows.value,
to_char(rows."time",'yyyy-mm-dd hh24:mi:ss') AS "time"
from
(
select
row_number() over (order by "time") as rown,
count(*) over () as cnt,
history.*
from history
where reference = :id and "time" between :start and :end
) rows
where mod(rows.rown - 1, ceil(rows.cnt / 100)) = 0;
These queries result in 100 records or a little less, depending on how many rows the table contains exactly. You can also use TRUNCATE(rowcount.cnt,0) instead of CEIL(rowcount.cnt) in MySQL, thus getting hundred rows or a little more and additionally apply LIMIT 100 to get exactly 100 rows (provided there are at least 100 rows in the table).
What you could is select the rowNumber and calculate the modulo of that rowNumber.
Not sure how it would be done in mysql but t-sql goes like this :
SELECT ROW_NUMBER() over( order by idField) % 50 as selector, *
FROM history
WHERE selector = 1
This will count the rows and reset the counter every 50th record, giving you a spread out result.

Sqlite Query select statement with sorted result respecting the OFFSET

I want to make a sqlite query in such a way that the result should be sorted which has a LIMIT and the OFFSET. But the OFFSET should work in synch a manner that it should discard the last records from the result.
SELECT * FROM TempTable WHERE CLASS = 1 ORDER BY Date ASC LIMIT 100 OFFSET 5;
The above query just ignores the first 5 records from the table and give the remaining records. But instead I want it to ignore the first 5 latest entries.
Note:- the first 5 latest entries means since I am sorting it by date it should IGNORE the latest record inserted in the table respecting the date.
Sort backwards, with OFFSET 5 and resort again:
SELECT * FROM (
SELECT * FROM TempTable WHERE CLASS = 1 ORDER BY Date DESC LIMIT 100 OFFSET 5
) ORDER BY Date ASC;

mysql select with this query then pad out with this query?

I am trying to do a mysql query to select some news stories from a table, now the key is I always need 5 results.
so I was hoping to be able to pad out my results with another where clause ie
select * from here where this = 1
if there is < 5 results then select * from here where this = 2
limit [how ever many we are short say the first query brings back 3 results this should bring back 2]
Now I've looked at using a union to do this but without outside help ie from php and another query to count the results I don't think it is possible, I know I could simply use php to do this, and will probably end up doing that, but I was just wondering if what I am trying to do is possible with one mysql query?
EDIT:
also it needs to order by date but they are not really posted in order so
order by date get upto 5 where this = 1 and if there isn't 5 pad it out with the remainder of where this = 2 also ordered by date.
Another Shameful Edit:
ask a silly question lol... it was my sleep deprivation I just assumed there was data in the table and the previous coder was using unions to do all sorts of stuff, making me think it was more complex than it should be
SELECT *
FROM
news
WHERE
( this = 45 || this= 0 )
AND
active = '1'
ORDER BY
this ASC,
date_added DESC
LIMIT 5
How about -
SELECT *
FROM here
WHERE this < 5 -- added this WHERE clause based on the idea that there will be at least one item per this
ORDER BY this ASC, `date` ASC
LIMIT 5;
Or are you after the five results then being sorted by date again -
SELECT *
FROM (
SELECT *
FROM here
WHERE this < 5 -- added this WHERE clause based on the idea that there will be at least one item per this
ORDER BY this ASC, `date` ASC
LIMIT 5
) AS tmp
ORDER BY `date` ASC
You could combine the where clauses and use limit :
select * FROM here WHERE this = 1 OR this = 2 ORDER BY this LIMIT 5
Even in there were 15 records where this is equal to 1 this would only bring back 5 records ...

Rotate table data with out update data

SELECT * FROM `your_table` LIMIT 0, 10
->This will display the first 1,2,3,4,5,6,7,8,9,10
SELECT * FROM `your_table` LIMIT 5, 5
->This will show records 6, 7, 8, 9, 10
I want to Show data 2,3,4,5,6,7,8,9,10,1 and
next day 3,4,5,6,7,8,9,10,1,2
day after next day 4,5,6,7,8,9,10,1,2,3
IS IT POSSIBLE with out updating any data of this table ???
You can do this using the UNION syntax:
SELECT * FROM `your_table` LIMIT 5, 5 UNION SELECT * FROM `your_table`
This will first select rows within your limit, and then combine the remainder from the second select. Note that you don't need to set a limit on the second select statement:
The default behavior for UNION is that duplicate rows are removed from the result. The optional DISTINCT keyword has no effect other than the default because it also specifies duplicate-row removal. With the optional ALL keyword, duplicate-row removal does not occur and the result includes all matching rows from all the SELECT statements.
I don't think this might be achieved using a simple Select (I may be wrong). I think you'll need a stored procedure.
You've tagged this as Oracle, though your SQL syntax would be invalid for Oracle because it doesn't support LIMIT
However, here's a solution that will work in Oracle:
select *
from ( select rownum as rn,
user_id
from admin_user
order by user_id
) X
where X.rn > :startRows
and X.rn <= :startRows + :limitRows
order by case when X.rn <= :baseRef
then X.rn + :limitRows
else
X.rn
end ASC
;
where :startRows and :limitRows are the values for your LIMIT, and :baseRef is a value between 0 and :limitRows-1 that should be incremented/cycled on a daily basis (ie on day 1 it should be 0; on day 2, 1; on day 10, 9; on day 11 you should revert to 0). You could actually use the current date, converted to Julian and take the remainder when divided by :limitRows to automate calculating :baseRef
(substitute your own column and table names as appropriate)
Well, it might be a little bit late for the author of the question, but could be useful for people.
Short answer: It is possible to do the "spin" like author asked.
Long answer: [I'm going to explain for MySQL first - where I tested this]
Let's imagine that we have table your_table (INT rn, ...). What you want is to sort in specific way ("spin" with beginning at the rn=N). First condition of ordering is rn >= N desc. The idea (at least how I understand this) is we change the order from asc to desc and split our table in two parts (<N and >=N). Then we order this back by rn but asc order. It will execute sorting for each group independently. So here is our query:
select * from your_table where rn between 1 and 10
order by rn >= N desc, rn asc;
If you don't have rn column - you always can use the trick with parameter
select t.*, #rownum := #rownum + 1 AS rn
from your_table t,
(SELECT #rownum := 0) r
where #rownum < 10 /* here be careful - we already increased by 1 the rownum */
order by #rownum >=N - 1 desc, /* another tricky place (cause we already increased rownum) */
#rownum asc;
I don't know if the last one is efficient, though.
For Oracle, you always can use rownum. And I believe that you will have the same result (I didn't test it!).
Hope it helps!