calculate time difference between two rows using datetime - date-difference

I would like to calculate the difference between two row using a time field. I have the query, but the difference is coming out in the second row and not the first. How to i mend so the difference starts from the first row :
SELECT DATEDIFF(second, pDataDate, dataDate)
FROM (
SELECT *,
LAG(dataDate) OVER (ORDER BY dataDate) pDataDate
FROM rows
) q
WHERE pDataDate IS NOT NULL

Related

Getting all the rows between first timestamp and the difference between first and second - MySQL

I have a requirement to get all the records between the times of two sign-ins of the user.
When we retrieve the row of the first sign in and get the first timestamp (t1), we want to get all the hits made by the user between this and the second timestamp (t2).
What I am doing is: getting the first timestamp (t1) and subtracting it with the second timestamp (t2) of the user. I'll then add the difference (t2-t1) with the first timestamp and run query to get all the hits between t1 and (t1+d).
Hence there are two things I am trying to do: (Getting second timestamp and the difference)
first timestamp (t1) is: 1507559316
SELECT
id, timestamp, (timestamp - 1507559316) as Difference
FROM
login_activity l
WHERE
l.uid=445 AND timestamp > 1507559316
ORDER BY
timestamp
LIMIT 1
Getting all the rows between first timestamp and the Difference
t1 = 1507559316
difference = 1226
SELECT
name, address, time
FROM
records r
WHERE
time BETWEEN FROM_UNIXTIME(1507559316) AND FROM_UNIXTIME(1507559316 + 1226)
ORDER BY
time
Do you think it's the right way to approach this?
Unless I seriously have misunderstood something, your method is unnecessarily complicated. You seem to have A and B and then are re-computing B by doing A + (B - A). This is mysterious.
Assuming that your first timestamp is in t1, you could simplify the first query to
SELECT
id, timestamp AS t2
FROM
login_activity l
WHERE
l.uid=445 AND t2 > t1
ORDER BY
timestamp
LIMIT 1
and your second query to
SELECT
name, address, time
FROM
records r
WHERE
time BETWEEN FROM_UNIXTIME(t1) AND FROM_UNIXTIME(t2)
ORDER BY
time
There is no need to re-compute t2 as t1 + (t2 - t1) and thus no need to compute the difference in the first place.

How to get max value of a column from two tables

I have one table named 'posts' and one table named 'threads'. Both have the column called 'total_id' which is an integer.
Now, how to get from those two tables the 1 highest value (max) in the 'total_id' column? (MySQL)
You can get it as follows:
SELECT Greatest(
(SELECT Max(total_id) FROM posts),
(SELECT Max(total_id) FROM threads)
)
Though it's not really very clear what's your expected result but in case you want max of total_id including both tables data? If yes, then you can do a UNION and then get the highest value like
select max(total_id) as max_total_id from (
select total_id from posts
union
select total_id from threads ) xxx;

Mysql select one more row for each group

Hi there is an exemple of the result that i want
no : order number
dataType : 1 = the number of piece done for a specific week
2 = the number of piece remaining
done : number of piece done
total : number of piece
I would like to make a single query. The first part of this query would return me all the rows with a dataType 1 and the other part would return me all rows with dataType 2. The dataType 2 would be there only once per orders, it is only there to show what is remaining to do.
I have done this query by duplicating my big select and using a UNION, but there is a way to use all the order number returned in the first query to get what is remaining to do for each order by adding a new row.
thank you I hope my question is clear
I have made an sqlfiddle to show you the query that i did
http://sqlfiddle.com/#!9/26a9d/15
SELECT
no,
SUM(vCountDone) done,
(SELECT SUM(vCountTotal) FROM table1) total,
week,
'1' AS dataType
FROM
table1
GROUP BY DATE(week)
UNION ALL SELECT
no,
SUM(vCountTotal) - SUM(vCountDone) done,
(SELECT SUM(vCountTotal) FROM table1) total,
MAX(week),
'2' AS dataType
FROM
table1 t2

Select row based on max date across several columns

For a mysql database
I have a table which includes duplicate rows because of date values in several columns. I am looking to select a single row for each unique customer id based on a max date value evaluated across several date columns
[customer id, startDate, StopDate, modifyDate, buyDate]
For each customer id, i'd like to return the row that has the maximum date either in the startDate, StopDate, modifyDate or buyDate columns ( there are some nulls in the date columns.
editing to include example - but to sure how to create a table here:
*** Edit
Been trying for quite awhile now to create a table here with an example. can't figure out. So posting an image? the desired rows to the returned indicated in red.
Assuming that the values are never NULL, you can use greatest():
select t.*
from table t
where greatest(t.startDate, stopDate, buyDate) =
(select max(greatest(t.startDate, stopDate, buyDate))
from t t2
where t2.customerid = t.customerid
);
Note: this will return multiple rows for a customer if more than one row contains the maximum date.

Fetch MySQL rows + one before condition

I have a time log table where all entries are entered with a time stamp and an event.
Now I want to select all the rows after a specific event AND ONE row before(ORDER BY time_stamp) that event.
I can easily achieve this with multiple queries, but is it possible with only one query ?
Using multiple queries
SELECT time
FROM table
WHERE event LIKE '%event_to_fint%'
SELECT event
FROM table
WHERE time<'time from last query'
LIMIT 1
ORDER BY
time DESC
One option is to
Use a regular select to select the time records you need.
Union this result with a select that retrieves the maximum time where this maximum time is less than the minimum time from your regular select.
SQL Statement
SELECT time
FROM table
WHERE event LIKE '%event_to_fint%'
UNION ALL
SELECT MAX(time)
FROM table
WHERE time < (
SELECT MIN(time)
FROM table
WHERE event LIKE '%event_to_fint%'
)
This solution in not too nice, but works:
SELECT *
FROM table
WHERE <condition_A>
ORDER BY <condition_B>
LIMIT (SELECT COUNT(*)
FROM table
WHERE <condition_A>
)+1