I have got a table with two columns. The first one ("val") is a integer, the second a timestamp ("ts").
Now I want to calculate the difference between the first and the last value of a given timespan.
SELECT MAX(val) - MIN(val) AS difference WHERE ts >= '2015-01-01 00:00:00' AND ts <= '2015-01-07 23:59:59'
This one is not sufficient, because in the course of time the values can exceed/undercut the first and the last value.
Example:
Day 1: 100
Day 2: 120
Day 3: 110
Day 4: 98
Day 5: 105
Day 6: 112
Day 7: 110
The difference is 110 (Day 7) minus 100 (Day 1) = 10. Not Max(val) = 120 minus Min(val) = 98 = 22
Thanks!
Join to a subquery that returns the first and last dates and join using those, and use some simple arithmetic to calculate the difference using sum():
select sum(val * case ts when ts1 then -1 else 1 end) diff
from data
join (select min(ts) ts1, max(ts) ts2
from data
where ts between ? and ?) x
on ts in (ts1, ts2)
See demo (demo schema has been simplified to isolate the essence of the solution).
One method is to use two subqueries, one that gets the first value and one that gets the last value:
SELECT ((SELECT val
FROM table t
WHERE ts >= '2015-01-01 00:00:00' AND ts <= '2015-01-07 23:59:59'
ORDER BY val DESC
LIMIT 1
) -
(SELECT val
FROM table t
WHERE ts >= '2015-01-01 00:00:00' AND ts <= '2015-01-07 23:59:59'
ORDER BY val ASC
LIMIT 1
)
) as difference
Related
This is my query
WHERE id = 14 AND start_time BETWEEN '2019-10-24 00:00:00' AND '2019-12-12 23:59:59'
ORDER BY created_date LIMIT 0 , 10
When I run this query then it returns me data of this data also. -> 2019-10-23T19:23:41.000Z
Issue: When I pass the 2019-10-24 then why it gives me data of 2019-10-23 date?
Note: start_time has a data type -> datetime in db.
It's not a issue by the way it's correct output.
Try this way
DATE_FORMAT(start_time, "%Y-%m-%d %H:%i:%s") as start_time
Due to diffrent formate, It may confused you.
use less than or equal to '<=' or greater than or equal to '>=' operator instead of BETWEEN.
Use this condition in your query.
WHERE id = 14 AND DATE(start_time) >= DATE('2019-10-24 00:00:00') AND DATE(start_time) <= DATE('2019-12-12 23:59:59') ORDER BY created_date LIMIT 0 , 10
OR
WHERE id = 14 AND DATE(start_time) >= '2019-10-24' AND DATE(start_time) <= '2019-12-12') ORDER BY created_date LIMIT 0 , 10
I'm trying to find to which shift belongs a datetime field.
Shifts are defines as time, and I have a startingHour and endingHour.
The query
SELECT * FROM shifts WHERE TIME('2009-11-20 06:35:00') BETWEEN '06:00:00' and '19:00:00'
works perfect, but when the shift is set to start 19:00:00 to 06:00:00 and the time is 23:35:00 it doesn't return anything
WHERE TIME('2009-11-20 23:35:00') BETWEEN '19:00:00' and '06:00:00'
that line isn't returning anything though I do have records on the table
Thanks
That's the shifts table.
if I query this:
SELECT
a.ID,
b.Nombre
FROM turnos a
JOIN operarios b ON a.oID = b.oId
WHERE a.uId = 1
AND (TIME('2019-11-22 18:23:00') BETWEEN a.horaInicio AND a.horaFin )
LIMIT 1
I get the proper result, but when I query this:
SELECT
a.ID,
b.Nombre
FROM turnos a
JOIN operarios b ON a.oID = b.oId
WHERE a.uId = 1
AND (TIME('2019-11-22 02:45:00') BETWEEN a.horaInicio AND a.horaFin )
LIMIT 1
I get no result.
These are two cases: start time < end time and start time > end time. You need something like this:
where (start_time < end_time and $t >= start_time and $t < end_time)
or (start_time >= end_time and ($t < start_time or $t >= end_time))
Since '19:00:00' is greater than '06:00:00' then:
BETWEEN '19:00:00' and '06:00:00'
returns 0 (false) and you get no rows.
One way to get the results that you want is to use CASE like this:
.................
AND 1 = CASE
WHEN a.horaInicio <= a.horaFin THEN TIME('2019-11-22 02:45:00') BETWEEN a.horaInicio AND a.horaFin
ELSE (TIME('2019-11-22 02:45:00') BETWEEN a.horaInicio AND '23:59:59')
OR (TIME('2019-11-22 02:45:00') BETWEEN '00:00:00' AND a.horaFin)
END
I am trying to calculate data from my database but first I've noticed strange behavior from the results I get, second, I have trouble making a request that take into account refills.
I have a table with :
Name - DateTime - content
I want to group by day the rows and select the difference of the number to have the consumption.
For example :
Name - DateTime - Content
Foo - 22-04-2018 6:00 - 120
Foo - 22-04-2018 10:00 - 119
Foo - 22-04-2018 16:00 - 118
The content has decreased, the result should be -2.
Output of my request = -2
Another example :
Name - DateTime - Content
Foo - 23-04-2018 6:00 - 50
Foo - 23-04-2018 10:00 - 90
Foo - 23-04-2018 16:00 - 120
Here we can notice that the number has increased. It means that instead of a consumption, we have refilled the reserve and the content has increased.
The result should be -70.
Output of my request : 30
My request :
SELECT day,
Abs(Sum(diffn)) AS totN
FROM (SELECT Date(datetime) AS day,
Max(content) - Min(content) AS diffN
FROM logs
WHERE NAME = 'Foo'
AND datetime >= '2018-04-22 00:00:00'
AND datetime <= '2018-04-23 00:00:00'
GROUP BY Date(datetime)) a
GROUP BY day;
But for the second example I have 30 as a result instead of 70, I don't know why...
I would like your help to change my request and take refills into account so that I get the results I want.
Thanks!
You need to determine the Prefix by comparing the highest and the lowest value, the time (hour) included. I'm using the 'CASE' function with two subqueries here.
Maybe you'll need to turn the year-month-day around, because I'm using the german datetime-format.
SET #datetime = '2018-04-22';
SELECT date(datetime) as day
,(CASE WHEN
(SELECT content FROM logs WHERE date(datetime) = #datetime ORDER BY datetime LIMIT 1)
>
(SELECT content FROM logs WHERE date(datetime) = #datetime ORDER BY datetime desc LIMIT 1)
THEN min(content) - max(content)
ELSE max(content) - min(content) END) as diffN
FROM logs
WHERE Name = 'Foo' AND date(datetime) = #datetime
GROUP BY day(datetime)
ORDER BY datetime
;
This should do the job:
SELECT day(datetime) as day, max(content) - min(content) as diffN
FROM logs
WHERE Name = 'Foo'
AND datetime >= '2018-04-23 00:00:00'
AND datetime <= '2018-04-24 00:00:00'
GROUP BY day(datetime)
Also, change the date filters it should be betweeen 23 and 24.
It might be that you need to establish the first and last datetime and their associated content. For example
drop table if exists t;
create table t (name varchar(3), dt datetime, content int);
insert into t values
('Foo' , '2018-04-22 06:00:00', 120),
('Foo' , '2018-04-22 10:00:00', 119),
('Foo' , '2018-04-22 16:00:00', 118),
('Foo' , '2018-04-23 06:00:00', 50),
('Foo' , '2018-04-23 10:00:00', 90),
('Foo' , '2018-04-23 16:00:00', 120);
select s.name,lastinday,firstinday,lastinday - firstinday
from
(
select name,dt, content lastinday
from t
where dt = (Select max(dt) from t t1 where t1.name = t.name and date(t1.dt) = date(t.dt))
) s
join
(
select name,dt, content firstinday
from t
where dt = (Select min(dt) from t t1 where t1.name = t.name and date(t1.dt) = date(t.dt))
) t
on t.name = s.name and date(t.dt) = date(s.dt);
+------+-----------+------------+------------------------+
| name | lastinday | firstinday | lastinday - firstinday |
+------+-----------+------------+------------------------+
| Foo | 118 | 120 | -2 |
| Foo | 120 | 50 | 70 |
+------+-----------+------------+------------------------+
2 rows in set (0.00 sec)
Why are you grouping it second time:
Ideally this should work:
SELECT Date(datetime) AS day,
Max(content) - Min(content) AS diffN
FROM logs
WHERE NAME = 'Foo'
AND datetime >= '2018-04-22 00:00:00'
AND datetime <= '2018-04-23 00:00:00'
GROUP BY Date(datetime)
Result of this query will contain only 2 rows - 1 for 22th and 1 for 23rd day. There is no need of grouping it again by day
I have a table value like
Id_indicator, Value , date_data
1 2 01/10/2016
1 2 03/10/2016
1 3 04/10/2016
1 2 05/10/2016
2 21 06/10/2016
2 21 07/10/2016
2 21 08/10/2016
3 3 09/10/2016
3 4 10/10/2016
3 4 11/10/2016
4 4 12/10/2016
I need to query that table and only need to get id_indicator and count(number or id_indicator)where value has not change over the time period.
I tried with group by query
select id_indicator, count(*), value
from shrewd_db.indicator_status_history
where date_data between '2016-10-01 00:00:00' and '2016-10-01 10:59:59'
group by id_indicator, value`;
now difficult to get only value that was unchanged like id_indicator = 2, value =21 and max (date_data) = 08/10/2016
SQL Fiddle Demo
I have to change the date range, otherwise only first row will be include it
SELECT `Id_indicator`, COUNT(*), max(value) as value
FROM Table1
WHERE `date_data` between '2016-10-01 00:00:00' and '2016-10-13 10:59:59'
GROUP BY `Id_indicator`
HAVING max(value) = min(value)
OUTPUT
This result also will include indicator with only one record, where obviously cant change value. You may want add HAVING COUNT(*) > 1 to remove those
If I understood you correctly and you want all the counts where the VAL didn't change from record to record :
SELECT p.id_indicator,count(*) as cnt
FROM (
SELECT t.id_indicator,
t.value
(SELECT s.value FROM YourTable s
WHERE s.id_indicator = t.id_indicator
AND s.date_data between '2016-10-01 00:00:00' and '2016-10-01 10:59:59'
AND s.date_data < t.date_data
ORDER BY s.date_data DESC
LIMIT 1) as last_val
FROM YourTable t
WHERE t.date_data between '2016-10-01 00:00:00' and '2016-10-01 10:59:59' ) p
WHERE p.val = p.last_val
GROUP BY p.id_indicator;
If you want only those that in the entire period the VAL didn't change then it's easier:
select id_indicator, count(*),max(DATE_DATA)
from shrewd_db.indicator_status_history
where date_data between '2016-10-01 00:00:00' and '2016-10-01 10:59:59'
group by id_indicator
HAVING MAX(VALUE) = MIN(VALUE);
I am new here and tried to look up the answer to my question but couldn't find anything on it. I am currently learning how to work with SQL queries and am wondering how I can count the amount of unique values that appear in two time intervals?
I have two columns; one is the timestamp while the other is a customer id. What I want to do is to check, for example, the amount of customers that appear in time interval A, let's say January 2014 - February 2014. I then want to see how many of these also appear in another time interval that i specify, for example February 2014-April 2014. If the total sample were 2 people who both bought something in january while only one of them bought something else before the end of April, the count would be 1.
I am a total beginner and tried the query below but it obviously won't return what I want because each entry only having one timestamp makes it not possible to be in two intervals.
SELECT
count(customer_id)
FROM db.table
WHERE time >= date('2014-01-01 00:00:00')
AND time < date('2014-02-01 00:00:00')
AND time >= date('2014-02-01 00:00:00')
AND time < date('2014-05-01 00:00:00')
;
Try this.
select count(distinct t.customer_id) from Table t
INNER JOIN Table t1 on t1.customer_id = t.customer_id
and t1.time >= '2014-01-01 00:00:00' and t1.time<'2014-02-01 00:00:00'
where t.time >='2014-02-01 00:00:00' and t.time<'2014-05-01 00:00:00'
Here's one method of doing this with conditional grouping in an inner-select.
Select Case
When GroupBy = 1 Then 'January - February 2014'
When GroupBy = 2 Then 'February - April 2014'
End As Period,
Count (Customer_Id) As Total
From
(
SELECT Customer_Id,
Case
When Time Between '2014-01-01' And '2014-02-01' Then 1
When Time Between '2014-02-01' And '2014-04-01' Then 2
Else -1
End As GroupBy
From db.Table
) D
Where GroupBy <> -1
Group By GroupBy
Edit: Sorry, misread the question. This will show you those that overlap those two time ranges:
Select Count(Customer_Id)
From db.Table t1
Where Exists
(
Select Customer_Id
From db.Table t2
Where t1.customer_id = t2.customer_id
And t2.Time Between '2014-02-01' And '2014-04-01'
)
And t1.Time Between '2014-01-01' And '2014-02-01'