Is it possible to calculate the time difference from a timestamp in the same field? My SQL knowledge isn't bad, but I can't figure out how I'd go about doing such a thing on the same table.
ID, SENSOR, COUNT, TIMESTAMP
'1461630', '1', '91', '2013-08-02 09:14:30'
'1461629', '1', '92', '2013-08-02 09:13:29'
'1461628', '1', '92', '2013-08-02 09:12:27'
'1461627', '1', '91', '2013-08-02 09:11:26'
'1461626', '1', '91', '2013-08-02 09:10:24'
'1461625', '1', '7', '2013-08-02 09:03:14'
'1461624', '1', '13', '2013-08-02 09:02:12'
'1461623', '1', '13', '2013-08-02 09:01:11'
'1461622', '1', '7', '2013-08-02 09:00:09'
'1461621', '1', '3', '2013-08-02 08:58:06'
What I need to do, is display a pie-chart with UP vs Down time values. I only have 1 table to reference, so it would all have to be on minute intervals, given only the timestamp above.
Specifically, with the times below, the machine wasn't running for around 7 mins. It's this 7 mins I have to figure out.
'1461626', '1', '91', '2013-08-02 09:10:24'
'1461625', '1', '7', '2013-08-02 09:03:14'
Is this possible? Or would I really need a blow-by-blow account in a separate table? Obviously, I'd rather not create more tables, because the device I'm working with is really quite limited, and it's already a massive hit for it to report this data to a tcp server I have running.
Obviously TIMESTAMPDIFF(,) doesn't work as I need two reference points, whereas I've only got the one. I'm imagining some kind of dodgy sub-select scenario, but I'm not sure.
Cheers!
You can self-join the table, something like this works in your case:
SELECT
*
FROM
yourTable a
INNER JOIN yourTable b ON a.ID = b.ID + 1
WHERE TIMESTAMPDIFF(second, a.timestamp, b.timestamp) > 60
But this can get ugly when you have gaps in your ID column. And especially it can get ugly (in terms of performance (when you don't have good indexes on the table)) when you have *lots of data.
So, I'd suggest using a bit more advanced queries using variables. Without the need to join the table to itself this typically runs pretty fast:
SELECT * FROM (
SELECT
yt.*,
TIMESTAMPDIFF(second, #prevTS, `timestamp`) AS timedifference,
#prevTS:=yt.`timestamp`
FROM
yourTable yt
, (SELECT #prevTS:=(SELECT MIN(`timestamp`) FROM yourTable)) vars
ORDER BY ID
)subquery_alias
WHERE timedifference > 65
see it working live in sqlfiddle
To further improve this query to display the two rows where timedifference is too big shouldn't be a problem :) When you get in serious trouble, feel free to ask, though.
Related
`ID`,`NAMES`, `TIMESTAMP`
'3', 'Sharon', '2020-02-17 21:11:12'
'4', 'RALPH', '2020-02-18 01:50:19'
Above is an extraction from my table.I would want to find time difference between the two timestamps(i.e for ID=3 and ID=4). Also i would like to create a column for the time difference.Please someone help
Thanks
You can use TIMESTAMPDIFF() function
This question already has answers here:
MySQL date format DD/MM/YYYY select query?
(9 answers)
Closed 3 years ago.
I have two MySQL database tabels (named 'accounts' and 'events') with some columns like events.date. This column has a 'date' format. It shows the date as i.e. '2020-02-17'. Now I want to show some data from this combined tables with a query and I want to convert the date formate to DD-MM-YYYY instead of YYYY-MM-DD. I've tried some things but I got errors.
Queries I've tried:
SELECT events.id, convert(varchar, events.date, 105), events.starttime, events.endtime, events.reason, events.created, events.employee, events.employee_id, REPLACE(REPLACE(REPLACE(accounts.location, '1', 'London'), '2', 'Birmingham'), '3', 'Rochdale') location, events.minutes, ROUND((minutes/60), 2) as hours FROM events JOIN accounts ON events.employee_id = accounts.id ORDER BY date
SELECT events.id, convert(date(4), events.date, 20), events.starttime, events.endtime, events.reason, events.created, events.employee, events.employee_id, REPLACE(REPLACE(REPLACE(accounts.location, '1', 'London'), '2', 'Birmingham'), '3', 'Rochdale') location, events.minutes, ROUND((minutes/60), 2) as hours FROM events JOIN accounts ON events.employee_id = accounts.id ORDER BY date
This query works well but shows me the wrong date format:
SELECT events.id, events.date, events.starttime, events.endtime, events.reason, events.created, events.employee, events.employee_id, REPLACE(REPLACE(REPLACE(accounts.location, '1', 'London'), '2', 'Birmingham'), '3', 'Rochdale') location, events.minutes, ROUND((minutes/60), 2) as hours FROM events JOIN accounts ON events.employee_id = accounts.id ORDER BY date
Can you please help me? Many thanks in advance!
You have to use DATE_FORMAT:
SELECT
events.id,
DATE_FORMAT(events.date, '%d-%m-%Y'),
events.starttime,
events.endtime,
events.reason,
events.created,
events.employee,
events.employee_id,
REPLACE(REPLACE(REPLACE(accounts.location, '1', 'London'), '2', 'Birmingham'), '3', 'Rochdale') location,
events.minutes,
ROUND((minutes/60), 2) as hours
FROM events
JOIN accounts ON events.employee_id = accounts.id
ORDER BY date
use FORMAT function
To get DD-MM-YYYY use
SELECT FORMAT (getdate(), 'dd-MM-yyyy ')
read more here: https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2012/hh213505(v=sql.110)
I have problem with count multiple fields within time range.
I have following table:
# date, count, fb_user_count, email_user_count, reg_dt
'2015-10-27', '11', '6', '5', '2015-11-02 13:59:14'
'2015-10-26', '3', '1', '2', '2015-11-02 13:59:10'
I want to get weekly number of registration with types, like this:
# date, count, fb_user_count, email_user_count, reg_dt
'2015-11-02', '31', '16', '15', '2015-11-02 13:59:14'
'2015-11-09', '12', '6', '6', '2015-11-09 13:59:14'
And monthly:
# date, count, fb_user_count, email_user_count, reg_dt
'2015-11', '131', 'x', 'y', '2015-11-02 13:59:14' (the last value is not so important)
'2015-12', '112', 'x', 'y', '2015-12-09 13:59:14'
I tried different approaches, but I struggle to finish this task. Any help will be great. Thanks!
You can use MySQL Date and Time functions WEEK(), MONTH() and YEAR() to build your queries :
SELECT
MIN(date),
SUM(count),
SUM(fb_user_count),
SUM(email_user_count)
FROM visit
GROUP BY WEEK(date, 1); -- First day is Monday
SELECT
CONCAT(YEAR(date), '-', MONTH(date)),
SUM(count),
SUM(fb_user_count),
SUM(email_user_count)
FROM visit
GROUP BY YEAR(date), MONTH(date);
For your weekly report (on mondays) you can use
select subdate(min(date), 6-weekday(min(date))),
sum(count), sum(fb_user_count), sum(email_user_count), max(reg_dt)
from tablename
group by subdate(date, 6-weekday(date));
and for your monthly report
select concat(year(max(date), '-', month(max(date)),
sum(count), sum(fb_user_count), sum(email_user_count), max(reg_dt)
from tablename
group by year(date), month(date);
I have a table of twitter data in MYSQL where the columns is_retweet, is_reply is made of binary values where 1=yes, 0=no. if a user retweeted multiple times in a day, there would then be multiple rows of ones in the retweet coulmn for that user on that day.
account_id, datetime, user_screenname, is_retweet, is_reply,followers_count
'9', '2008-06-11 20:06:35','Access2', '1', '0', '811'
'9', '2008-06-11 23:06:35','Access2', '1', '1', '812'
'9', '2008-06-12 20:01:21','Access2', '0', '1', '813'
'7', '2008-06-11 17:01:00','actingparty', '1', '1', '2000'
I rearrange my sql output to a table below which tells me: for a username on any day, what is the total number of retweets, replies and highest follower count.
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count
'9', '2008-06-11', 'Access2', '2', '0', '812'
'9', '2008-06-12', 'Access2', '0', '1', '813'
Here is my sql code:
CREATE VIEW `tweet_sum` AS
select
`tweets`.`account_id` AS `account_id`,
`tweets`.`user_screenname` AS `user_screenname`,
CAST(`tweets`.`datetime` as date) AS `period`,
MAX(`tweets`.`followers_count`) AS `followers_count`,
SUM(`tweets`.`is_reply`) AS `sum_reply`,
SUM(`tweets`.`is_retweet`) AS `sum_retweet`,
from
`tweets`
group by cast(`tweets`.`datetime` as date), tweets.username
Ultimately, I want to have one more column Reach (which is equal to followers_count times the number of columns(is_retweet, is_reply) that is greater than zero.)
For example, in the output table below, the sum_retweet and sum_reply columns are both greater than zero for 2008-06-11 so i will need to take followers_count*2=1624 for the reach column.
How can i structure my sql code to do that?
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count, **Reach**
'9', '2008-06-11', 'Access2', '2', '1', '812', '1624'
'9', '2008-06-12', 'Access2', '0', '1', '813', '813'
I thought of doing it this way:
1.create a new view
2.count the number of columns that have values >0
3.then take that number multiply by followers count for that day
And the code for that below:
CREATE VIEW tweet_reach AS
SELECT
COUNT(t.sum_reply,t.sum_retweet,t.sun_mention,t.sum_direct,t.sum_mytweet)*t.followers_count AS Reach
FROM information_schema.columns
WHERE table_name='tweet_sum' t AND
t.sum_reply>0 OR
t.sum_retweet>0 OR
t.sun_mention>0 OR
t.sum_direct>0 OR
t.sum_mytweet>0;
This code is wrong but hoping to do something like this. Is it possible?
Thanks,
J
You can do this easily by adding a column in your existing view:
CREATE VIEW `tweet_sum` AS
select `tweets`.`account_id` AS `account_id`,
`tweets`.`user_screenname` AS `user_screenname`,
CAST(`tweets`.`datetime` as date) AS `period`,
MAX(`tweets`.`followers_count`) AS `followers_count`,
SUM(`tweets`.`is_reply`) AS `sum_reply`,
SUM(`tweets`.`is_retweet`) AS `sum_retweet`,
MAX(`tweets`.`followers_count`) * ((SUM(`tweets`.`is_reply`) > 0) + (SUM(`tweets`.`is_retweet`) > 0)) as reach
from `tweets`
group by cast(`tweets`.`datetime` as date), tweets.username;
MySQL treats a boolean expression such as x = y as the integer 1 when true and 0 when false. So, you can just add them together for your multiplication factor.
I have a table of twitter data in MYSQL where the columns is_retweet, is_reply is made of binary values where 1=yes, 0=no. if a user retweeted multiple times in a day, there would then be multiple rows of ones in the retweet coulmn for that user on that day.
account_id, datetime, user_screenname, is_retweet, is_reply,followers_count
'9', '2008-06-11 20:06:35','Access2', '1', '0', '811'
'9', '2008-06-11 23:06:35','Access2', '1', '1', '812'
'9', '2008-06-12 20:01:21','Access2', '0', '1', '813'
'7', '2008-06-11 17:01:00','actingparty', '1', '1', '2000'
How should i structure my SQL view to give me a result like the table below where i can sum up the retweets and replies for any given day, and by username?
IE What i am trying to do is:
-for a username on any day, what is the total number of retweets, replies and highest follower count.
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count
'9', '2008-06-11', 'Access2', '2', '0', '812'
'9', '2008-06-12', 'Access2', '0', '1', '813'
Here is my sql code:
CREATE VIEW `tweet_sum` AS
select
`tweets`.`account_id` AS `account_id`,
`tweets`.`user_screenname` AS `user_screenname`,
CAST(`tweets`.`datetime` as date) AS `period`,
MAX(`tweets`.`followers_count`) AS `followers_count`,
SUM(`tweets`.`is_reply`) AS `sum_reply`,
SUM(`tweets`.`is_retweet`) AS `sum_retweet`,
from
`tweets`
group by cast(`tweets`.`datetime` as date)
However my data dont seem to match with what i want as it seems that the sql is summing up all users retweets for that day. How can i group it by day and username as well?
Thanks!
J
******EDIT*************************************
I would like to extend the question. Say I have one more column Reach (which is equal to followers_count times the number of columns(is_retweet, is_reply) that is greater than zero.)
For example, in the output table below, the sum_retweet and sum_reply columns are both greater than zero for 2008-06-11 so i will need to take followers_count*2=1624 for the reach column.
How can i structure my sql code to do that?
account_id, date, user_screenname, sum_retweet, sum_reply, followers_count, **Reach**
'9', '2008-06-11', 'Access2', '2', '1', '812', '1624'
'9', '2008-06-12', 'Access2', '0', '1', '813', '813'
just change your GROUP BY to
group by
`tweets`.`account_id`,
`tweets`.`user_screenname`,
cast(`tweets`.`datetime` as date)