Average result of last 10 rows - sqlite

I'm trying to find the average figure for the last 10 rows in a database table:
select avg(Reading)
from Readings
Order By Rowid
desc limit 10;
This pulls the average of all entries in the table, not the last 10. I've tried all sorts of variations but can't get it to work.

Thanks for the super quick replies, I tried again and managed to type in the correct syntax this time in the From clause.
Here is the correct answer:
select avg(Reading)
from(select Reading
from Readings
Order By Rowid desc
limit 10);

Related

Select Max Count of consecutive rows with same value mariaDB mysql

I got a table like this, and i want to get the largest consecutive count of rows where win=1 for each puuid and ordered by game_creation DESC. I tried many solution of the web but can't get any of them to work, maybe somebody here can help me.

SQLite3 closest timestamp

I have a large SQLite3 database with 30 tables the biggest one being a GPS data table with strings stored every second more or less. Other tables are in the range of 500-3000 record each. I am trying to extract from one of the tables all the records and link them to the coordinates, latitude and longitude from the GPS table based on a matching timestamp in the format yyyy-mm-dd hh:mm:ss.sss occurring in both tables. My current string is
SELECT *
FROM gpsData, Sightings
WHERE strftime('%Y-%m-%d %H:%M:%S',gpsData.UTC) = strftime('%Y-%m-%d %H:%M:%S',Sightings.UTC);
This gives me what I need in a reasonable amount of time. The only problem is that in the GPS table I have some missing values, I guess for the GPS not having good coverage, and therefore I get many Records with NULL values. Is there a way to modify my statement so that I can search for the exact match or the closest one however back in time?
I have tried with:
SELECT *
FROM gpsData, Sightings
WHERE strftime('%Y-%m-%d %H:%M:%S',gpsData.UTC) >= strftime('%Y-%m-%d %H:%M:%S',Sightings.UTC);
or
SELECT *
FROM gpsData, Sightings
WHERE strftime('%Y-%m-%d %H:%M:%S',gpsData.UTC) <= strftime('%Y-%m-%d %H:%M:%S',Sightings.UTC);
With no success. At the moment I am running the following:
SELECT *
FROM gpsData, Sightings
ORDER BY ABS(strftime('%Y-%m-%d %H:%M:%S',gpsData.UTC) - strftime('%Y-%m-%d %H:%M:%S',Sightings.UTC)) ASC
I don't know if it is working cause the query has been running for the last one and half hours...
How can I modify my statement to get what I need in an efficient way? I have tried a few solutions proposed elsewhere but they don't seem to work for me. I am totally new to SQLite. Thanks for your help.

How to find the nth largest value of each row in SQL

I have researched this problem and have found the answer for a single query, where you can find the nth value of a single column by using DESC OFFSET 2. What I am trying to do is find the nth value for each item in a row. For example, I'm working with a data base concerning bike share data. The data base stores the duration of each trip and the date. I'm trying to find the 3rd longest duration for each day in a data base. If I was going to find the max duration I would use the following code.
SELECT DATE(start_date) trip_date, MAX(duration)
FROM trips
GROUP BY 1
I want the output to be something like this.
Date 3rd_duration
1/1/2017 334
1/2/2017 587
etc
If the value of the third longest duration is the same for two or more different trips, I would like the trip with the lowest trip_id to be ranked 3rd.
I'm working in SQLite.
Any help would be appreciated.
Neither SQLite nor MySQL have a ROW_NUMBER function built in, so get ready for an ugly query. We can still group by the date, but to find the max duration we can use a correlated subquery.
SELECT
DATE(t1.start_date) AS start_date,
t1.duration
FROM trips t1
WHERE
(SELECT COUNT(*) FROM trips t2
WHERE DATE(t2.start_date) = DATE(t1.start_date) AND
t2.duration <= t1.duration) = 3;
Note that this approach might break down if you could have, for a given date, more than one record with the same duration. In this case, you might get multiple results, neither of which might actually be the third highest duration. In order to handle such ties, you should tell us what the logic is with regard to ties.
Demo here:
Rextester

HOW TO SELECT RANDOM ROWS IN mb maria

SELECT col1 FROM tbl ORDER BY RAND() LIMIT 10;
This can work fine for small tables. However, for big table, it will have a serious performance problem as in order to generate the list of random rows, MySQL need to assign random number to each row and then sort them.
Even if you want only 10 random rows from a set of 100k rows, MySQL need to sort all the 100k rows and then, extract only 10 of them.
My solution for this problem, is to use RAND in the WHERE clause and not in the ORDER BY clause. First, you need to calculate the fragment of your desired result set rows number from the total rows in your table. Second, use this fragment in the WHERE clause and ask only for RAND numbers that smallest (or equal) from this fragment.
SELECT col1 FROM tbl WHERE RAND()<=0.0005;
In order to get exactly 100 row in the result set, we can increase the fragment number a bit and limit the query:
For example:

sqlite subqueries with group_concat as columns in select statements

I have two tables, one contains a list of items which is called watch_list with some important attributes and the other is just a list of prices which is called price_history. What I would like to do is group together 10 of the lowest prices into a single column with a group_concat operation and then create a row with item attributes from watch_list along with the 10 lowest prices for each item in watch_list. First I tried joins but then I realized that the operations where happening in the wrong order so there was no way I could get the desired result with a join operation. Then I tried the obvious thing and just queried the price_history for every row in the watch_list and just glued everything together in the host environment which worked but seemed very inefficient. Now I have the following query which looks like it should work but it's not giving me the results that I want. I would like to know what is wrong with the following statement:
select w.asin,w.title,
(select group_concat(lowest_used_price) from price_history as p
where p.asin=w.asin limit 10)
as lowest_used
from watch_list as w
Basically I want the limit operation to happen before group_concat does anything but I can't think of a sql statement that will do that.
Figured it out, as somebody once said "All problems in computer science can be solved by another level of indirection." and in this case an extra select subquery did the trick:
select w.asin,w.title,
(select group_concat(lowest_used_price)
from (select lowest_used_price from price_history as p
where p.asin=w.asin limit 10)) as lowest_used
from watch_list as w

Resources