Find out max dates of all users in same table - mysql

I have a table data with multiple imei. I want to find out all max(dates) of all imei.
I am writing this query:
SELECT *
from data
where gpsdatetime=(
select max(gpsdatetime)
from data)
AND imei in("+imei_string+");
But this fetches all the data of selected imei and then selects only one max date from all the data.
I want to find out all max(dates) of all imei.

Try this:
SELECT imei, MAX(gpsdatetime)
FROM data
GROUP BY imei
This query makes use of the GROUP BY clause.
Demo on SQLFiddle.
If you want all imei rows (even duplicates) then use the following query:
SELECT a.imei, b.gpsdatetime
FROM data a
JOIN (
SELECT imei, MAX(gpsdatetime) as gpsdatetime
FROM data
GROUP BY imei
) b
ON a.imei = b.imei
Demo on SQLFiddle.

I think you are looking for this
SELECT * From data D
where gpsdatetime in (select max(gpsdatetime) from Data group by imei)
AND imei in("+imei_string+");

Related

SQL to retrieve id that match the highest date, grouped by another field

I have the following data:
I want to retrieve the ids that match the highest date (represented by max(folga)), grouping by funcionario_id.
Here's the output table I want, but it's missing the id:
How can I achieve this?
Thanks for the attention.
One way to do it is with NOT EXISTS:
select
t.funcionario_id, t.folga, t.id
from tablename t
where not exists (
select 1 from tablename
where funcionario_id = t.funcionario_id and folga > t.folga
)
or you can group by funcionario_id first to get the max date (I guess this is the query that returned the result you posted) and then join to the table:
select t.*
from tablename t inner join (
select funcionario_id, max(folga) folga
from tablename
group by funcionario_id
) g on g.funcionario_id = t.funcionario_id and g.folga = t.folga

Group all row in single row for context data in SQL

I am trying to group single user data which is spread in multiple row in mySQL.
I have tried using group by, Joining multiple times all 4 column but not able to achieve.
Below is my Table and Schema
and I Am trying to get it in the below form as Result
Can you please help me to form a query in MYSQL
Join to the table all the max columns you get after grouping by deviceid:
select distinct
t.date,
t.deviceid,
g.country,
g.affid,
g.accountid,
g.package
from tablename inner join (
select
deviceid,
max(country) country,
max(affid) affid,
max(accountid) accountid,
max(package) package
from tablename
group by deviceid
) g on g.deviceid = t.deviceid
You can do aggregation :
select deviceid, max(country), max(affid), max(accountid), max(package)
from table t
group by deviceid;
EDIT : If you are working with higher version then you can use window function :
select distinct t.Date, t.deviceid,
max(t.country) over (partition by t.deviceid) as country,
max(t.affid) over (partition by t.deviceid) as deviceid,
. . .
from table t;

Get distinct rows along with sum of field

My table structure is like this shown in below URL:
http://tinypic.com/r/sm3xbb/8
I want to write a MySQL query to get sum of count field of all such rows which have same hash value but have distinct date value.
This can be done with Group by statement but I won't get all rows if I will perform group by on hash field.
Example: Rows with ID 1,4 and 8 have same hash value in table, so I want to retrieve all 3 rows along with SUM of count field.
Another way to do it with a subquery
SELECT t.*,
(
SELECT SUM(count)
FROM table1
WHERE hash = t.hash
) total
FROM table1 t
Here is SQLFiddle demo
You can try something like this
select t.*,t1.sum_count
from mytable t
join
(
select hash, sum(`count`) as sum_count
from mytable group by hash
) t1 on t.hash = t1.hash

Get last but one row for each ID

I am using query like
select * from audittable where a_id IN (1,2,3,4,5,6,7,8);
For each ID its returning 5-6 records. I wanted to get the last but one record for each ID.
Can i do this in one sql statement.
Try this query
SELECT
*
FROM
(SELECT
#rn:=if(#prv=a_id, #rn+1, 1) as rId,
#prv:=a_id as a_id,
---Remaining columns
FROM
audittable
JOIN
(SELECT #rn:=0, #prv:=0) t
WHERE
a_id IN (1,2,3,4,5,6,7,8)
ORDER BY
a_id, <column> desc)tmp --Replace column with the column with which you will determine it is the last record
WHERE
rId=1;
If your database is having DateCreated or any column in which you are saving the DateTime as well like when your data is inserted for a particular row then you may use query like
select at1.* from audittable at1 where
datecreated in( select max(datecreated) from audittable at2
where
at1.id = at2.id
order by datecreated desc
);
You may also use LIMIT function as well.
Hope you understand and works for you.
In SQLite, you have the columns a_id and b. For each a_id you get a set of b's. Let you want
to get the latest/highest (maximum in terms of row_id, date or another naturally increasing index) one of b's
SELECT MAX(b), *
FROM audittable
GROUP BY a_id
Here MAX help to get the maximum b from each group.
Bad news that MySQL doesn't associate MAX b with other *-columns of the table. But it still can be used in case of simple table with a_id and b columns!

2 rows having unique field and recent date

Table :
I am new to query writing. Now I am stuck on retrieving 2 rows from above table.
Data will be date sorted in descending order for only 2 different topic_id. There won't be a third different topic_id.
So I want to retrieve two rows only that will have different topic_id, one data for each topic_id having most recent date.
The result would be
try sql fiddle
http://sqlfiddle.com/#!2/f37963/9
SELECT t1.* FROM temp t1
JOIN (SELECT question_id, MAX(`date`) as `date` FROM temp GROUP BY topic_id) t2
ON t1.question_id= t2.question_id AND t1.`date`= t2.`date`;
The logic is to find the latest date in each group (subquery) and join it with the table again to retrieve other particulars.
use this
$qry="SELECT * FROM table_name GROUP BY TOPIC_ID ORDER BY DATE desc";