MySQL - How to subtract two datetime from the same table - mysql

I need help on a small problem with a subtraction in the same table and column
Well, iam creating a view, but the aplication generated the results of used time in tha same table and column.
My table have the following columns: id,field_id,object_id and value_date.
| ID | FIELD_ID | OBJECT_ID | VALUE_DATE |
| 55 | 4 | 33 | 2016-12-18 19:02:00 |
| 56 | 5 | 33 | 2016-12-18 19:12:00 |
| 57 | 4 | 35 | 2016-12-18 19:30:00 |
| 58 | 5 | 35 | 2016-12-18 20:00:00 |
I do not have much knowledge in sql, but i have tried some functions like timestampdiff, period_siff and others examples in stackoverflow.com.
Someone help me to subtract ID 56 with field_id 5 by line with ID 55 and field_id 4 in object_id 33 in SQL to bring the result in minutes. Ex: 10 or 00:10:00
An article about this problem would already help me. Thank you very much!

Lets assume that you want result to be in day format then query will be :
SELECT DATEDIFF(day,startDate,endDate) AS 'Day'
FROM table1;
Find complete example here

The soluction is below:
select TIMESTAMPDIFF(MINUTE,F1.value_date,F2.value_date) as minutes, F1.value_date,F2.value_date,F1.object_id,F2.object_id,F1.field_id,F2.field_id
from otrs_tst.dynamic_field_value F1
join otrs_tst.dynamic_field_value F2 on F1.object_id = F2.object_id
where F1.field_id in ('4','5')
and F2.field_id in ('4','5')
and F2.field_id <> F1.field_id
and F1.field_id < F2.field_id
group by F1.object_id,F2.field_id

Related

SQL - select x entries within a timespan

I'm creating a database (in MySQL) with a table of measurements. For each measurement I want to store the DateTime it came in. For showing plots within an app for different intervals (measurements of the day/week/month/year) I want sample the data points I have, so I can return e. g. 30 data points for the whole year as well as for the day/hour. This is the same as done with stock price graphs:
stock price plot for 1 day
vs
stock price plot for 1 month
As you can see, the amount of data points is the same in both pictures.
So how can I select x entries within a timespan in MySQL via SQL?
My data looks like this:
+====+====================+=============+==========+
| id | datetime | temperature | humidity |
+====+====================+=============+==========+
| 1 | 1-15-2016 00:30:00 | 20 | 40 |
+----+--------------------+-------------+----------+
| 2 | 1-15-2016 00:35:00 | 19 | 41 |
+----+--------------------+-------------+----------+
| 3 | 1-15-2016 00:40:00 | 20 | 40 |
+----+--------------------+-------------+----------+
| 4 | 1-15-2016 00:45:00 | 20 | 42 |
+----+--------------------+-------------+----------+
| 5 | 1-15-2016 00:50:00 | 21 | 42 |
+----+--------------------+-------------+----------+
| 6 | 1-15-2016 00:55:00 | 20 | 43 |
+----+--------------------+-------------+----------+
| 7 | 1-15-2016 01:00:00 | 21 | 43 |
+====+====================+=============+==========+
Let's say, I always want two data points (in reality a lot more). So for the last half hour I want the database to return data point 1 and 4, for the last ten minutes I want it to return 6 and 7.
Thanks for helping!
PS: I'm sorry for any errors in my English
OK, assuming a very simple systematic approach, you can get the first and last entry for any defined period:
select *
from table
where mydatetime =
(select
max(mydatetime)
from table
where mydatetime between '2017-03-01' and '2017-03-15'
)
OR mydatetime =
(select
min(mydatetime)
from table
where mydatetime between '2017-03-01' and '2017-03-15'
)
I believe your answer can be found at the following location:
https://stackoverflow.com/a/1891796/7176046
If you are looking to filter out any items not within your date/time your query would use:
Select * from table where Date/Time is (What you want to sort by)

mysql 5.5.50 Calculate one day multiple events to one

I have a problem with my thinking and I must ask someone to help.
I create simply working time tracking system and I can manage all other things with that code, but I cannot now figure out how I can calculate following data.
Fist, I have couple tables and one store four things.
index | who | timestamp | what
1 | 1 | 2016-09-21 08:00:00 | Work
2 | 2 | 2016-09-21 08:01:00 | Work
3 | 1 | 2016-09-21 10:00:00 | Bin
4 | 2 | 2016-09-21 10:00:00 | Bin
5 | 1 | 2016-09-21 10:15:00 | Bout
6 | 2 | 2016-09-21 10:17:00 | Bout
7 | 2 | 2016-09-21 13:00:00 | Bin
8 | 1 | 2016-09-21 13:00:00 | Bin
9 | 1 | 2016-09-21 13:30:00 | Bout
10 | 2 | 2016-09-21 13:30:00 | Bout
11 | 2 | 2016-09-21 15:58:00 | Home
12 | 1 | 2016-09-21 16:05:00 | Home
I can nicely calculate times between Work and Home and got right value to the right person.
But I'm stuck now with those break times.
I need calculate all possible breaks together per person and that way get a total time what is spend to breaks per person.
So I need something like following answer when ask person 1 info:
Who | Time | Breaktime | Working time
1 | 08:05:00 | 00:45:00 | 07:20:00
Or maybe all persons can came to same page when ask specific day...
Who | Time | Breaktime | Working time
1 | 08:05:00 | 00:45:00 | 07:20:00
2 | 07:57:00 | 00:47:00 | 07:10:00
There is always pairs with events. Work -> Home and Bin -> Bout.
And yes, there is much more persons and could be much more brake times per person.
That might be a bad presentation, sorry about that (NooB). I hope I give that much information as someone can help. But ask if there is something to ask.
That is code what I use when I solve one day total time at working place.
SELECT TIMEDIFF(
(SELECT timestamp FROM `stamps` WHERE (who like '1' and DATE (timestamp) like '2016-09-21' and what like 'Home')),
(SELECT timestamp FROM `stamps` WHERE (who like '1' and DATE (timestamp) like '2016-09-21' and what like 'Work'))
)
But I cannot use it with multiple events.
That is what I found. Maybe not nice, but it works =).
$break = mysqli_query($conn,"SELECT what, timestamp FROM stamps WHERE (who like '$id' and DATE (timestamp) like '$day' and what like 'B%') ORDER BY timestamp");
while($row = mysqli_fetch_array($break))
{
if ( $row['what'] == "Bin" )
$start = strtotime( $row['timestamp'] );
else { $stop = strtotime( $row['timestamp'] );
$hours += ( $stop - $start );
}
}
$btime = gmdate('H:i:s', floor($hours));
$btime gives 00:00:00 style result.

Select statement inside SSRS textbox

I am trying to assign values to textboxes in SSRS but am running into a bit of trouble.
My dataset "dsStepInfo" groups and sums a number of rows together based on "ID", query below.
SELECT ID, SUM(Target) AS Target, SUM(Actual) AS Actual
FROM tblStepComplete
WHERE (CompleteID = #ParamID)
GROUP BY ID
And returns:
ID Target Actual
-------------------------------
| 10 | 44418 | 44418 |
-------------------------------
| 12 | 13193 | 13123 |
-------------------------------
| 22 | 1411 | 1411 |
-------------------------------
| 50 | 160 | 80 |
-------------------------------
| 52 | 68 | 34 |
-------------------------------
| 101 | 12120 | 12119 |
-------------------------------
| 105 | 875 | 868 |
-------------------------------
| 140 | 40 | 40 |
-------------------------------
| 560 | 2985 | 3418 |
-------------------------------
I want to assign cells in the grid in the picture to a certain ID. The Cell for tank 110 would always be the Target of ID=50 and Actual of ID=50 for example. Many of the IDs returned aren't going to be populated in the table, just the 10 displayed in this table. Is there any way to perform a SELECT, or equivalent in the textbox as an expression to get these specific values from the dataset?
You can use the Lookup function.
This should get the ID #50 and output its Target:
=Lookup(50, Fields!ID.Value, Fields!Target.Value, "DSStepInfo")
I think if it were me I'd just use a Case statement, something like
SELECT
Case [ID]
WHEN 10 Then 'Tank 50'
WHEN 12 Then 'Tank 120'
When 22 Then 'Tank 130'
WHEN 50 Then 'Tank 140'
When 52 Then 'Tank 150'
End As TankNo
,SUM([Target]) As Target
,SUM([Actual]) As Actual
FROM tblStepComplete
WHERE (CompleteID = #ParamID)
Group By [ID]
And then build your report using the values presented. Better would be an additional table that had the Tank names linked to the IDs you are pulling, but if you don't have that then the Case statement is fairly easy to maintain.

MYSQL SUM() isnt adding correctly

Well I guess I shouldn't say it isn't adding correctly, but more like the SUM() function is not producing the results I am expecting to get. The problem I have is with a table that tracks hours for employees.
Here is the data from the table:
table name = labor_table
employee_id | labor_date | hours
252 | 2015-04-30 | 8
252 | 2015-05-01 | 8
252 | 2015-05-04 | 8
252 | 2015-05-24 | 0.2
So if I run this query on that table
SELECT employee_id,
SUM(`hours`) AS hours
FROM (`labor_table`)
WHERE `employee_id` = '252'
GROUP BY employee_id
I get the Following result:
employee_id | hours
252 | 24.200000002980232
Can anyone tell me why this is happening?
the field "hours" is set as a FLOAT, could that be the reason for the extra digits?

Find Max number from unique values in SQL Stored Procedure

Hi I'm trying to get a unique list of ID values based on the latest (max) version number
My table looks like:
id Version Cost Name Status
---|-----|------------|--------|---------
35 | 1.0 | 200000 | john | Open
36 | 1.0 | 400000 | juliet | Open
35 | 2.0 | 350000 | borat | Closed
36 | 1.5 | 30000 | john | Waiting Update
I want it to be able to return
id Version Cost Name Status
---|-----|------------|--------|---------
35 | 2.0 | 350000 | borat | Closed
36 | 1.5 | 30000 | john | Waiting Update
I've tried using this but can't get it to work and I'm not sure where I'm going wrong
select FB.* from CSLL.Feedback FB
inner join (
select ID
,max(Version)
,Status as MaxID
from CSLL.Feedback
group by ID
) groupedID
ON FB.ID = groupedID.ID
AND FB.Version = groupedID.MaxID
and FB.Status = groupedID.Status
Which was based on a response to another question found here
LINKY
Can anyone help at all?
Many thanks in advance
Tom
Try this.
select fb1.* from Feedback fb1
LEFT JOIN Feedback fb2 on fb2.id=fb1.id and fb2.version>fb1.version
where fb2.id IS NULL;