I have a simple problem; one which I thought would have a simple solution. Imagine a Table like this:
Name Timestamp Data
Bob 2011-01-01 01:00:00 Hi
Alice 2011-02-02 02:00:00 Hello
Alice 2011-03-03 03:00:00 Hello
Bob 2011-04-04 04:00:00 Bye
Charlie 2011-05-05 05:00:00 Cheese
Charlie 2011-06-06 06:00:00 Toast
All I want is to be able to run a query that shows the most recent entry for each Name. So with the above table, I would like an output like this:
Name Timestamp Data
Bob 2011-04-04 04:00:00 Bye
Alice 2011-03-03 03:00:00 Hello
Charlie 2011-06-06 06:00:00 Toast
Ordered by Data. I can't figure out how to do this. I though I could just do:
SELECT DISTINCT(Name), timestamp, Data FROM Table ORDER BY Data
But this doesn't work. Any help would be most appreciated,
Cheers.
Use this query to get last unique values,
SELECT * FROM (select * from `table` order by date DESC) t group by name
SELECT DISTINCT Name, Timestamp, Data
FROM Table
WHERE Timestamp =
(
SELECT MAX(Timestamp)
FROM Table AS tempTable
WHERE Table.Name = tempTable.Name
)
select t1.* from table as t1
inner join (
select name,max(`timestamp`) as `timestamp`
from table
group by name) as t2
on t1.name = t2.name and t1.`timestamp` = t2.`timestamp`
Related
I am trying to select from a table like this below and limit it by the latest date.
record_date
Name
Age
2021-05-19
John
21
2021-05-19
Smith
25
2021-05-19
Mary
20
2021-05-20
Ann
26
2021-05-20
Jane
23
I tried doing
SELECT * FROM table WHERE DATE(record_date) BETWEEN MAX(DATE(record_date)) AND MAX(DATE(record_date))
The above code does not work, but this is what I am trying to acheive below:
record_date
Name
Age
2021-05-20
Ann
26
2021-05-20
Jane
23
I don't want to hard-code the date into the statement, because records come in everyday, so it has to be able to select the latest date and limit only that. Thanks!
Something like:
select * from table where record_date = (select max(record_date) from table)
One more query using JOIN
select t.*
from t
join (select max(record_date) max_date from t) max_date_t on record_date = max_date;
MySQL fiddle
bear with me, this is probably super easy but I just started SQL recently.
So I currently have a table that looks something like this:
SELECT startDate FROM table1
GROUP BY startDate
startDate
2020-01-01
2020-02-01
2020-03-01
But I'm trying to expand this into something like this, hopefully with a list of strings defined in the query itself:
startDate
rank
2020-01-01
Rank 1
2020-01-01
Rank 2
2020-02-01
Rank 1
2020-02-01
Rank 2
2020-03-01
Rank 1
2020-03-01
Rank 2
From there I have some calculations in mind that I want to do.
Is there any good way to actually do this?
Cross join the table with a synthesized table containing the ranks.
SELECT t1.startDate, t2.`rank`
FROM table1 AS t1
CROSS JOIN (
SELECT 'Rank 1' AS `rank`
UNION
SELECT 'Rank 2'
) AS t2
I need to split one date record into a several date records (dynamically, depending on a date range that overlaps) like this in MySQL:
input data:
level |start_time |end_time
1 2018-12-24 09:00:00 2018-12-25 09:00:00
Output result should look like:
level |start_time |end_time
1 2018-12-24 09:00:00 2018-12-25 00:00:00
1 2018-12-25 00:00:00 2018-12-25 09:00:00
Any help is much appreciated.
Try to initiate your sql code like this. It's a long shot but I hope it get you started somewhere.
select tx.col1
FROM
( (
SELECT start_time as col1 from t1
)
UNION
(
SELECT end_time as col1 from t1
)
) as tx
I have a MySQL table that comprises uid, login/logout status flat, timestamp, for example:
uid flag time
123 login 10:00:00
123 logout 10:10:00
321 login 10:08:00
321 logout 10:18:00
Now I want to display online player count for every minute between 10:08 to 10:09. What is expected:
Minute current_player_cnt
10:07 1
10:08 2
10:09 2
I can export data as csv file and do this using python, but I want directly do this using sql and cannot find a function that could help.
You do not need to give the entire sql sentence, just give a function name will be fine. Any suggestion is appreciated.
One option is to join using a calendar table, e.g.
SELECT
t1.Minute,
COUNT(t2.uid) AS current_player_cnt
FROM
(
SELECT '10:07:00' AS Minute UNION ALL
SELECT '10:08:00' UNION ALL
SELECT '10:09:00'
) t1
LEFT JOIN
(
SELECT uid, MIN(time) AS login_time, MAX(time) AS logout_time
FROM yourTable
GROUP BY uid
) t2
ON t1.Minute BETWEEN t2.login_time AND t2.logout_time OR
(t1.Minute >= t2.login_time AND t2.login_time = t2.logout_time)
GROUP BY
t1.Minute;
Demo
I want to ask about mysql query
I have table data like this
My data
`userid | time| date
609 | 07:56:31|2014-01-23
609 | 21:20:47|2014-01-23
609 | 17:25:27|2014-01-24
609 | 17:25:29|2014-01-24
609 | 17:13:54|2014-01-27
609 | 17:13:56|2014-01-27
609 | 07:55:09|2014-01-27`
I've tried various ways
but have not been successful
like set #start='1900-01-01';
set #finish='2033-12-31';
SELECT data.userid,t.clock_out,data.date from data
inner join (select max(time)as clock_out,date,userid from data where time>='10:01' group by data.userid,tgl)t
on(data.date = t.date and data.userid=t.userid and t.clock_out=date.time)
rule
clock_in where time<='10:00' and clock_out time>'10:00',clock_in or clock_out can fill in the blank/empty,,the date for example 25 and 26 is not input data,but can be retrieved from variable #start='1900-01-01'and #finish='2033-12-31';
and result would be
userid|date |clock_in | clock_out
609 |2014-01-23 |07:56 |21:20
609 |2014-01-24 | |17:25
609 |2014-01-25 | |
609 |2014-01-26 | |
609 |2014-01-27 |07:55 |17:13
. Can someone help me....:( thanks.in advance....
I think this is what you want, it isn't exactly as your answer but I think this is correct.
You need to join in a list of all unique dates for that user id and then use that list to group by and get min() and max() from time.
select t1.userid,
t2.date,
min(t1.time) as in_time,
max(t1.time) as out_time
from test t1
join (select distinct date from test where userid = 609) t2
where t1.date = t2.date
and userid = 609
group by t1.userid, t2.date;
I would also say that it seems far better to instead of a date and a time just store a datetime and then group by some function that selects the date-part.
http://sqlfiddle.com/#!2/7b8732/1
Have you tried GROUP_CONCAT?
SELECT user_id, date, GROUP_CONCAT(time) as in_out FROM data GROUP BY date ORDER BY date,time;
This isn't 100% what you need, but might be close enough?