Group by and Order by latest - mysql

I want to group by id and order by begintime(latest row).
data:
begintime id type created_at
2012-12-13 01:00:00 1 type1 2012-12-01 11:39:47
2012-12-13 00:00:00 1 type2 2012-12-02 00:29:10
2012-12-13 03:00:00 2 type3 2012-12-05 00:20:43
2012-12-13 02:00:00 2 type2 2012-12-06 11:31:41
2012-12-13 00:00:00 2 type1 2012-12-07 00:58:11
I want to get:
begintime id type created_at
2012-12-13 01:00:00 1 type1 2012-12-01 11:39:47
2012-12-13 03:00:00 2 type3 2012-12-05 00:20:43
How can I get it?

Your title contradicts from the question you have written. It says LATEST but you really want the first created_AT for every ID. Anyway,
You could use a subquery which separately gets the minimum created_AT for every ID. The result of the subquery is then joined back to get the values of the other columns.
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT ID, MIN(created_AT) max_date
FROM TableName
GROUP BY ID
) b ON a.ID = b.ID AND
a.created_AT = b.max_date
SQLFiddle Demo

Try this:
SELECT *
FROM table_name
GROUP BY id
ORDER BY
id ASC,
begintime DESC;
Hope it helps!

Related

Group by month with date time in sql

halo i want to count the data and group by month (month start date) then show in date time, any idea ?
table_a
timestamp
2020-11-28 04:00:00
2020-11-28 05:00:00
2020-12-29 01:00:00
2020-12-29 02:00:00
2020-12-29 03:00:00
expected result:
timestamp count
2020-11-01 00:00:00 2
2020-12-01 00:00:00 3
my query is:
SELECT STR_TO_DATE(DATE_FORMAT(timestamp, '%Y-%m-dd HH'), '%Y-%m-dd HH'), count(*) as count
from table_a
but my results was:
timestamp count
2020-11-00 2
2020-12-00 3
You can use:
select str_to_date(concat_ws('-', year(timestamp), month(timestamp), 1)) as yyyymm,
count(*)
from table_a
group by yyyymm;
i fixed the question with add group by month(timestamp) , i m ignored the minuted, the important things is group by month , here are my full query :
SELECT timestamp, count(*) as count
from table_a
group by month(timestamp)
then the output show :
timestamp count
2020-11-01 04:00:00 2
2020-12-01 01:00:00 3

How to get difference based on date in mysql

I have like below mentioned two table:
Table1
ID Unique_Value
T-1 OI-45
T-4 OI-45
T-8 OI-45
T-7 OI-46
T-6 OI-49
Table2
ID Date Value
T-1 2018-01-01 15:13:22 10
T-4 2018-03-15 18:10:45 15
T-8 2018-05-12 05:17:43 25
T-7 2018-04-01 15:13:22 12
T-6 2018-06-01 15:13:22 18
I have joined the Table2 ID with Table1 ID and get the Unique_Value, based on the unique value and order by Date in Descending order and group by Unique_Value, I want to get the difference value of a particular ID from the previous Value.
Required Output would be:
ID Date Value Unique_Value Difference
T-1 2018-01-01 15:13:22 10 OI-45 [Null]
T-4 2018-03-15 18:10:45 15 OI-45 5
T-8 2018-05-12 05:17:43 25 OI-45 10
T-7 2018-04-01 15:13:22 12 OI-46 [Null]
T-6 2018-06-01 15:13:22 18 OI-49 [Null]
I have tried using Lead Log but it didn't worked.
You can try below using lag() function - it will work for mysql version 8.0+
DEMO
select id,Date,value,Unique_Value,case when prevval is null then null else value-prevval end as Difference
from
(
select t1.Id,t1.Unique_Value,t2.Date,t2.value,lag(t2.value,1) over(partition by t1.Unique_Value order by t2.Date) as prevval
from table1 t1 inner join table2 t2 on t1.id=t2.id
)A
For Mysql Version 5.7 you can try below -
DEMO
SET #quot=0, #latest=0, #comp=''
select id, Unique_Value,d,value,case when latest=1 then c=null else c end as difference
from
(
select id,Unique_Value,d,value,c,IF(#comp<>Unique_Value,1,0) as LATEST,#comp:=Unique_Value as company from
(
select t1.Id,t1.Unique_Value,value,t2.d,value-#quot as c,#quot:=value
from t1 inner join t2 on t1.id=t2.id
order by t1.Unique_Value,t2.d
)A order by Unique_Value,d
)B
OUTPUT:
id d value Unique_Value Difference
T-1 2018-01-01 10 OI-45
T-4 2018-03-15 15 OI-45 5
T-8 2018-05-12 25 OI-45 10
T-7 2018-04-01 12 OI-46
T-6 2018-06-01 18 OI-49

mysql select 2 table based on date and combine each other

i Need some help.. i have some case like this below
i have 2 table.. call ("in_table" and "out_table")
data "in_table" look like
stock_id item_id date qty_in
-----------------------------------------
1 11 2017-07-11 12
2 11 2017-07-11 10
3 12 2017-07-11 10
4 12 2017-07-19 10
And i have "out_table" is like
id_tr item_id date qty_out
-------------------------------------
1 11 2017-07-19 2
1 12 2017-07-19 1
2 11 2017-07-19 2
2 12 2017-07-19 1
And i want to combine the date and display all the data like this,
Update: the join is by item_id but i want to select by date
date item_id qty_in qty_out
---------------------------------------
2013-07-11 11 22 0
2013-07-11 12 10 0
2013-07-19 11 0 4
2013-07-19 12 10 2
Thank you for your help.
It looks like you need kind of a full outer join of two aggregate subqueries. But in your case I would get item_id and date in a union subquery (derived table) and the sums in correlated subqueries (subselect).
select item_id, date,
(select sum(qty_in) from in_table i where i.item_id = sub.item_id and i.date = sub.date) as qty_in,
(select sum(qty_out) from out_table o where o.item_id = sub.item_id and o.date = sub.date) as qty_out
from (
select item_id, date from in_table
union
select item_id, date from out_table
) sub
order by date, item_id

Show Null Value from 2 comparable records in MySQL

I have example data table from attendance machine system like this:
ID Name In
1 John 2015-04-17 08:00:00
1 John 2015-04-17 16:30:00
1 John 2015-04-20 10:01:00
1 John 2015-04-21 10:00:00
1 John 2015-04-21 19:00:00
Here my query:
SELECT a.id AS ID, a.nama AS `Name`, DATE_FORMAT(a.att, '%d-%b-%y') AS `Date`,
DATE_FORMAT(a.att, '%T') as `IN`, DATE_FORMAT(b.att, '%T') AS `OUT`
FROM attendance a
INNER JOIN attendance b
on date(a.att) = date(b.att) AND a.id = b.id
WHERE b.att > a.att
With above query I can split the In (column) time into 2 column become In and out like this:
Id Name Date In Out
1 John 17-Apr-15 08:00:00 16:30:00
1 John 21-Apr-15 10:00:00 19:00:00
The problem is if user John forget to use finger print where he out, for example on date 20 April, the data is not display. So, I want to my output become like this:
Id Name Date In Out
1 John 17-Apr-15 08:00:00 16:30:00
1 John 20-Apr-15 10:01:00 NULL
1 John 21-Apr-15 10:00:00 19:00:00
You should use a LEFT JOIN instead of an INNER JOIN. Or you can use this query instead:
SELECT
Id,
Name,
DATE(att) AS `Date`,
DATE_FORMAT(MIN(att), '%T') AS `In`,
CASE WHEN MAX(att)<>MIN(att) THEN DATE_FORMAT(MAX(att), '%T') END AS `Out`
FROM
attendance
GROUP BY
Id, Name, DATE(att)
Please see a fiddle here.

Select last entry for every user from log table

Hello I have problem with sql that is suppose to select last log entry per user for many users.
This is sample how my table looks like
user_id | date
--------------------
1 2013-03-06 10:00:00
17 2013-03-06 11:00:00
2 2013-03-06 10:00:00
5 2013-03-06 10:00:00
1 2013-03-06 11:00:00
17 2013-03-06 13:00:00
17 2013-03-06 13:01:00
2 2013-03-06 14:01:00
2 2013-03-06 15:00:00
2 2013-03-06 18:01:00
The result of this query is suppose to be
user_id | date
--------------------
1 2013-03-06 11:00:00
2 2013-03-06 18:01:00
5 2013-03-06 10:00:00
17 2013-03-06 13:01:00
For now I'm using this query
SELECT
a.user_id,
(SELECT b.date FROM alerts.alerts_log as b WHERE b.user_id = a.user_id ORDER BY b.date DESC LIMIT 1) as date
FROM
alerts.alerts_log as a
WHERE
a.user_id IN (1,2,5,17)
GROUP BY a.user_id
but I dont thiknk its the best one because will be too slow for many records...
can you pls suggest me better one
SELECT user_id , MAX(date)
FROM alerts.alerts_log
GROUP BY user_id;
if you have only two columns in the table, the query below will suffice your needs,
SELECT user_ID, MAX(date) max_date
FROM tableName
GROUP BY user_ID
SQLFiddle Demo
but if not and you want to get all columns within the row, you need to have subquery which separately gets the maximum date per user_ID.
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT user_ID, MAX(date) max_date
FROM tableName
GROUP BY user_ID
) b ON a.user_ID = b.user_ID AND
a.date = b.max_date
SQLFiddle Demo