SQL Query for timestamp - android

I had following Table
CREATE TABLE Customer
( `Name` varchar(7), `Address` varchar(55), `City` varchar(15),`Contact` int,`timestamp` int)
;
INSERT INTO Customer
(`Name`,`Address`, `City`, `Contact`,`timestamp`)
VALUES
('Jack','New City','LA',79878458,456125),
('Joseph','New Lane23','LA',87458458,794865),
('Rosy','Old City','Paris',79878458,215125),
('Maria','New City','LA',79878458,699125),
('Jack','New City','LA',79878458,456125),
('Rosy','Old City','Paris',79878458,845125),
('Jack','New Main Street','New York',79878458,555525),
('Joseph','Near Bank','SAn Francisco',79878458,984521)
;
I want to get all customer record with highest timestamp without duplication.

Try the following.
select name,max(timestamp),Address,City,Contact from Customer group by name

I want to get all customer record with highest timestamp without
duplication.
Use DISTINCT operator and ORDER BY clause like
select distinct `Name`,`Address`, `City`, `Contact`,`timestamp`
from customer
order by `timestamp` desc;
In that case you can use JOIN query like
select t1.*
from customer t1 join
(select Name, max(`timestamp`) as maxstamp
from customer
group by Name) xx
on t1.Name = xx.Name
and t1.`timestamp` = xx.maxstamp;

Try this:
SELECT * FROM `customer`
group by name,Address,City,Contact,timestamp
order by timestamp desc

I'm joining the Customer table with itself, the condition c1.timestamp<c2.timestamp on the join clause combined with c2.timestamp IS NULL
will make sure that only the latest record for each person is returned. I put DISTINCT because on your sample data there are two records for Jack with the same timestamp:
SELECT DISTINCT
c1.*
FROM
Customer c1 LEFT JOIN Customer c2
ON c1.Name=c2.Name
AND c1.Contact=c2.Contact -- you might want to remove this
AND c1.timestamp<c2.timestamp
WHERE
c2.timestamp IS NULL
ORDER BY
Name, Address
Please see a fiddle here.

Related

SQL Query, How to find the first and last value in the given table need by sequential order with conditions

I have ids in my table, ids start from 1 to 20, I want a query, to find the first and last records in a given table but I want the result by some condition.
For example: if I have the record
1,2,3,4,5,9,10,11,12,13, 19,20
I need a result like 1-5, 9-13, 19-20 like this I need results
This is the island part of the classic gaps and islands problem (With the gaps part being finding the missing values in between each island). If you search for that term, you'll find a ton of material about how to calculate them.
One approach (Requires Sqlite 3.25 or newer for window function support):
sqlite> CREATE TABLE ex(id INTEGER PRIMARY KEY);
sqlite> INSERT INTO ex VALUES (1),(2),(3),(4),(5),(9),(10),(11),(12),(13),(19),(20);
sqlite> WITH cte AS (SELECT id, id - row_number() OVER (ORDER BY id) AS grp FROM ex)
...> SELECT min(id) AS rangestart, max(id) AS rangeend FROM cte GROUP BY grp;
rangestart rangeend
---------- ----------
1 5
9 13
19 20
SQL Query to find first record in your table:
SELECT * FROM <table_name> ORDER BY <column_name> ASC LIMIT 1
SQL Query to find last record in your table:
SELECT * FROM <table_name> ORDER BY <column_name> DESC LIMIT 1
For example: if I have the record 1,2,3,4,5,9,10,11,12,13, 19,20
I need a result like 1-5, 9-13, 19-20 like this I need results
If you need result like you have mentioned, then you can set LIMIT in your query to get how many records you can have in that query.
QUERY:
SELECT * FROM <table_name> LIMIT <any_number>

How to store only 10 last records to room?

I have some Entity to store my data into ROOM. How to store only 10 last row to my db using room. For now I'n using #Query("SELECT * FROM Entity LIMIT 10") but it's dont looks right
We you want only first 10 or less records in your database then you have to set the id as auto increment and try to delete all those records where the ids doesn't match the first 10 results (after every insertion)
DELETE FROM tableName where id NOT IN (SELECT id from tableName ORDER BY id DESC LIMIT 10)
Here is a link to explore more:-
Limit the amount of rows in a room database
Does it work?
This is the correct way to do it:
SELECT expressions
FROM tables
[WHERE conditions]
[ORDER BY expression [ ASC | DESC ]]
LIMIT number_rows [ OFFSET offset_value ];
And here's a real example:
SELECT contact_id, last_name, first_name
FROM contacts
WHERE website = 'TechOnTheNet.com'
ORDER BY contact_id ASC
LIMIT 5;
I got these from https://www.techonthenet.com/sql/select_limit.php.
There could by slight differences in syntax as I do not know which type of SQL you are using.

Android : sqlite join result

I have a table which has one column's name and guestof .
I want to return a cursor to get three values : name guestOf and guestcount where guestcount is how many rows has "name" as the value of guestOf.
How can combine this internal query and return the result. Trying something like this, can it be done without inner query for better performance
SELECT name, guestCount from Table
as A outer join (SELECT guestOf , count(*)
AS GuestCount FROM Table group by guestOf)
as GuestCounts WHERE A.id = GuestCounts.guestOf
Give this code a go. I think it matches what you're looking for:
SELECT g.name, g.guestof,
(SELECT COUNT(guestof)
FROM guests AS c
WHERE g.name == c.guestof
) as guestcount
FROM guests AS g;
And as an example: SQLFiddle

display record in asc order using group_concat in sqlite android

I want to write an sqlite query in which a record will be displayed for example i have a contact table and account table i want to fetch a acount name and contact name from table i want to group_concat the contact name and it should not be repeated so i return a query:
select a.account_name, group_concat(DISTINCT c.contact_name) from account_table a join contact_table c on a.account_id = c.account_id;
this query execute perfectly know what i want to do is to get group_concat distinct name in asc order so i written the query :
select a.account_name, group_concat(DISTINCT c.contact_name order by c.contact_name) from account_table a join contact_table c on a.account_id = c.account_id;
its giving me error at order by
10-25 10:29:25.601: E/SQLiteLog(2214): (1) near "order": syntax error
can any one tell me how to solve the error.
You can't use ORDER BY clause inside aggregate function call.
Try:
select a.account_name, group_concat(DISTINCT c.contact_name)
from account_table a
join contact_table c on a.account_id = c.account_id
order by c.contact_name;

SQLite: Return the most recent 'n' rows per unique value of column 'y'

I have a table with the following schema:
CREATE TABLE table (
msg_id TEXT,
thread_id TEXT,
.
.
.
date INTEGER
)
I need to retrieve the most recent n msg_id per unique value of thread_id. Is there a way to do it using a single query or will I need to query the database to get the most recent distinct thread_ids, then query the database again PER unique thread_id? I recall reading somewhere that multiple database queries can get expensive.
You could use a correlated subquery. For example, for N = 5 :
select *
from YourTable yt1
where 5 <
(
select count(*)
from YourTable yt2
where yt2.thread_id = yt1.thread_id
and yt2.msg_id < yt1.thread_id
)
This is not too fast, so you might be better of with multiple queries.

Categories

Resources