Retrieve data according to timestamp value from Room Database - android-room

I'm trying to retrieve entries that were written in the last 2 minutes into the database.
I use the following query:
#Query("SELECT * FROM Contacts where ('now()' - gatt_server_connection_timestamp) <= 120000")
List<Contact> getContactsByGattServerConnectionTimestamp();
However the result I get is the whole database.
What is wrong with this query?

The SQLite date-time functions are described here.
If gatt_server_connection_timestamp is in milliseconds since epoch, this query should work:
#Query("SELECT * FROM Contacts where gatt_server_connection_timestamp >= (1000 * strftime('%s', datetime('now', '-2 minutes'))))
List<Contact> getContactsByGattServerConnectionTimestamp();

Related

Elastisearch sql how to bucket time

elasticsearch = 7.16.1
using this in python, this elasticsearch sql query seems to work to get the data I want within the time range:
es.sql.query(body={'query':"select * from \"index-*\" where \"#timestamp\" >= CAST('2022-06-30T08:00:00.000Z'AS DATETIME) and \"#timestamp\" <= CAST('2022-07-10T08:00:00.000Z'AS DATETIME) order by \"#timestamp\" desc "})
But it's returning all rows within that time.
I want to be able to get bucket by minutes, hours, or day. So it returns less rows basically but I can still get the right totals
Couldn't find where to do that here:
https://www.elastic.co/guide/en/elasticsearch/reference/current/sql-functions-datetime.html

How to return all the rows in tracker_sessions table using antonioribeiro/tracker

I am using antonioribeiro/tracker for my website analytics.
Everything is working as intended but I want to count all the rows in tracker_sessions table, when I do
$sessions = Tracker::sessions();
return count($sessions);
It returns only the number of rows created today.
How would I return all the rows in tracker_sessions table?
Looking at the documentation, you can do this to get more sessions:
$sessions = Tracker::sessions(60 * 24 * 365 ); // get sessions (visits) from the past 365 days
.., or any number of minutes you want.
You can also use Query Builder to count the table directly:
use Illuminate\Support\Facades\DB;
$sessions = DB::table('tracker_sessions')->count();

Count the average of number of surveys per year, Spring Data Jpa

I have a Model, called Survey that has a
#CreationTimestamp
#Temporal(TemporalType.TIMESTAMP)
private Date submittedDate;
field, which is a timestamp that shows the time that a row is insterted in this table.
What I want to be able to query and show is this :
The total number of rows for every month, 12 months.
And then find the average. Which would be :
(countJan+CountFeb+...)/12; ( cycle through countInEveryMonth, and divide by 12 )
How do I query this using Spring Data Jpa ?
Do I write a method like :
int countBySubmittedDate(Date submittedDate) ? -
but this will count the number of Surveys at a given timestamp?
Thank you.
Whole year average can be:
public interface SurveyRepository extends CrudRepository<Survey, Integer> {
#Query("SELECT COUNT(s) FROM Survey s WHERE (date_field BETWEEN :yearStart AND :yearEnd)")
Long yearAverage(#Param("yearStart") Date yearEnd, #Param("yearEnd") Date yearEnd);
//you may divide it by 12
}
Same can be done for other scenario.

Linq query within a date range on several tables?

I'm trying to find a way to detect spam in my SignalR chat application. Every time a user sends a messsage I would like to check the amount of messages sent by that user the last 5 seconds. I have a database with 2 tables: Message and User, where messages gets logged with a MessageDate and users with UserID. There is a Many-to-One relationship between the tables (1 user per message, several messages per user).
How can I write a query to check for messages sent by a specific user the last 5 seconds?
I have tried looking for a solution online but I'm new to queries and it's hard to get everything right (the join, the range of dates, using count property and getting data models right).
The closest I've gotten is something like :
var db = new MessageContext();
int messageCount = (from op in db.Message
join pg in db.User on op.UserID equals pg.UserID
where pg.UserID == op.UserID
&& (a.Start.Date >= DateTime.Now.AddSeconds(-5)
&& a.Start.Date <= DateTime.Now)
select op)
.Count();
Thanks in advance, any help appriciated!
Actually i don't see why you need a join at all, if you have a many to many Relationship you shouldn't need a join (you should have navigation properties) so the code should look like this:
var q = db.Users
.Select(usr=>
new
{
User = usr,
LastMessages = usr.Messages
.OrderByDescending(msg=>msg.Date)
.Take(5)
})
.Where(usr=>usr.LastMessages.All(msg=>msg.UpdateDate >= 5 minutes from now)
// Here q contains all the users that have posted 5 messages or more in the last 5 minutes as well as those last messages.

Combining all dates, and data, within a month!

I am trying to combine all days of each month into a date.
My query as off now:
SELECT
inventory_items.acquired_at AS Date_Acquired,
products.name AS products_name,
SUM(inventory_items.primary_quantity) AS inventory_items_primary_quantity
FROM
inventory_items inventory_items INNER JOIN customers customers ON inventory_items.source_id = customers.id
INNER JOIN products products ON inventory_items.product_id = products.id
GROUP BY
MONTH(Date_Acquired),
products_name
ORDER BY
MONTH(Date_Acquired)
I have a general idea of what to do, but not really sure how to implement it.
As I understand you and your Date_Acquired is an instance of sql Date type
you can gat day of months as pasting below code inside a textfield
(new SimpleDateFormat("d")).format(new java.util.Date())
which suppose to give you numbers like 1,2,3,...18,19...
Extra:
(new SimpleDateFormat("M")).format(new java.util.Date()) for month
(new SimpleDateFormat("yyyy")).format(new java.util.Date()) for year
(new SimpleDateFormat("d")).format(new java.util.Date())+" - "
+(new SimpleDateFormat("M")).format(new java.util.Date()) for getting a value like 28 - 01
What database? A typical SQL database result can only contain one data value per field. So you will not be able to retrieve all the products.name values in one result grouped by the month. If you retrieve all the results under a specified month you can aggregate them later on.

Resources