MySQL - Trying to get minimum value for each day - mysql

Got a table that holds dates and values, looks like this:
...Date........Value
2016-10-01 2.3
2016-10-01 1.6
2016-10-01 7.0
2016-10-02 2.4
2016-10-02 1.9
2016-10-02 7.3
etc, so multiple dates with multiple values for each date.
I can't figure out how to write the SQL to get to return a single row with the minimum value for each day.
Tried this but get error:Error in query (1054): Unknown column 'm.seldate' in 'on clause'
SELECT DISTINCT seldate
FROM mytable s
LEFT
JOIN
( SELECT MIN(myval) minval
FROM mytable
GROUP
BY seldate
) m
ON s.seldate = m.seldate

The following will return the minimum myvalue and the corresponding 'description' column (which I omitted in the original post) for each unique value of seldate within the table:
SELECT seldate, description, myvalue
FROM selections s1
WHERE myvalue=(SELECT min(s2.myvalue)
FROM selections s2
WHERE s1.seldate = s2.seldate);

Try:
select seldate, min(myval) as minval
from mytable
group by seldate

Related

SQL, find min and max of any symbol that max is more greater than 20 percent and newer from min

I need maximum and minimum in IBP column of live_filter table for each symbol, that occurred in the last 20 minute and the maximum is newer and greater than 20 percent of minimum. I've tried this script but can not figure out why it is listing maximum occurred before minimum?
SELECT * FROM (
SELECT i18 as symbol, MAX(ibp)/min_ibp AS ibp_rate
FROM live_filter
INNER JOIN (
SELECT i18 AS min_i18, Min(ibp) AS min_ibp, ct AS min_ct FROM live_filter
GROUP BY i18
) AS min_table
ON i18= min_table.min_i18
GROUP BY i18 ORDER BY ibp_rate DESC) AS t1
WHERE t1.ibp_rate > 1.2;
table would be like this:
ct: #creation time of log
ibp: # some values
i18: # symbol name
EDIT:
as #strawberry commented I've add a MRE:
create table IF NOT EXISTS live_filter (
ibp double,
ct datetime,
i18 varchar(50)
);
insert into live_filter (ibp, ct, i18) VALUES
('0.303', '2021-01-16 11:59:46', 'skhnd'),
('0.137', '2021-01-16 12:01:46', 'skhnd'),
('0.903', '2021-01-16 11:57:46', 'sds'),
('0.203', '2021-01-16 11:55:46', 'sds');
desired result is just like this:
|symbol | ibp_rate |
|sds | 4.448275862068965|
SQL Fiddle
EDIT2: desired result is to return no row because min comes after max.
EDIT3: change MRE to MCRE !!

How to sum daily resetting data using MySQL

I am attempting to plot data cumulatively from a MySQL table which logs a value, resetting to 0 every day. After selecting the values using select * from table where DateTime BETWEEN DateA AND DateB, the data looks like this: current data. I would like the output to look like this: preferred data, ignoring the daily resets.
As I am a novice in SQL I was unable to find a solution to this. I did, however, obtain the correct output in Matlab using a for loop:
output = data;
for k=1:(size(data, 1)-1)
% check if next value is smaller than current
if data(k+1)<data(k)
% add current value to all subsequent values
output = output + (1:size(data, 1)>k)'.*input(k);
end
end
I would like the final product to connect to a web page, so I am curious if it would be possible obtain a similar result using only SQL. While I have tried using SUM(), I have only been able to sum all values, but I need to add the last value each day to all subsequent values.
Using CTE and comparing dates, you can sum all values each date.
Let's say that table1 below is defined.
create table table1 (col_date date, col_value int);
insert into table1 values
('2020-07-15',1000),
('2020-07-15',2000),
('2020-07-16',1000),
('2020-07-16',3000),
('2020-07-16',4000),
('2020-07-17',1000),
('2020-07-18',2000),
('2020-07-19',1000),
('2020-07-19',1000),
('2020-07-19',2000),
('2020-07-19',3000),
('2020-07-20',4000),
('2020-07-20',5000),
('2020-07-21',6000)
;
In this case, the query looks like this:
with cte1 as (
select col_date, sum(col_value) as col_sum from table1
where col_date between '2020-07-16' and '2020-07-20'
group by col_date
)
select a.col_date, max(a.col_sum), sum(b.col_sum)
from cte1 a inner join cte1 b on a.col_date >= b.col_date
group by a.col_date;
The output is below:
col_date |max(a.col_sum) |sum(b.col_sum)
2020-07-16 |8000 | 8000
2020-07-17 |1000 | 9000
2020-07-18 |2000 |11000
2020-07-19 |7000 |18000
2020-07-20 |9000 |27000
The column of max() is just for reference.

MySQL: How to get values from Multiple Columns in one column

I have 2 tables with data. "keywords" and "Data".
key_id (P)
key_word
key_prod (I)
kay_country
key_is_wr
and
id (P)
dat_id (I)
dat_date
dat_is_weekly
For each keyword there are a few different rows linked in the data table - with different or the same dates in dat_date. I want to get all variations of dates that exist in this table only once so i can display them all in an array on my PHP code.
This is the query im currently using - it gets me a list of the same dates from the specific "key_app" value I need. But I only want to get an array of dates that exist for this specific key_id.
SELECT * FROM `keywords`
JOIN `data`
ON `keywords`.`key_id` = `data`.`dat_id`
WHERE `keywords`.`key_app`= 10
I Just haven't figure out yet how to:
1. Get only the dat_date section (I guess i need data.dat_date instead of the "*" in my query.
2. How to get only the different dates that exist for each key_prod.
I tried using all sorts of ORDER BY and MAX(id) and nothing seem to work well in terms of sytaxes
SELECT * FROM `keywords`
JOIN (
SELECT MAX(id) AS id
FROM `data`
GROUP BY `data`.`dat_date`
) `keywords`
ON `keywords`.`key_prod`=10
I have been digging it for a while but no right result
Example:
If I have under dat_date the vars
2016-06-21
2016-06-21
2016-06-21
2016-06-22
2016-06-22
2016-06-23
2016-06-23
for a specific key_app - I will get:
2016-06-21
2016-06-22
2016-06-23
as a query result
Try this.
select distinct data.dat_date
FROM `keywords`, `data`
ON `keywords`.`key_prod` = `data`.`dat_id`
WHERE `keywords`.`key_app`= 10
Your first query was how to display only the date. This can be done using select data.dat_date instead of select *.
Distinct will list only distinct dates not the same dates which was your query 2.

Mysql join query failure

For the tables below I need to output the rows that are not in the member_booking table from master_vehicle_inventory table.
I also need the results from master_vehicle_inventory which don't fall between the range of mb_startdate and mb_returndate.
master_vehicle_inventory table
mvi_id
1
2
3
4
member_booking table
mb_id mb_startdate mb_returndate mvi_id
100 22-04-2016 30-04-2016 2
101 23-01-2016 02-05-2016 3
So far I have tried this:
SELECT
mb.mb_id,
mb.mb_startdate,
mb.mb_returndate,
mvi.mvi_id
FROM master_vehicle_inventory AS mvi LEFT JOIN LEFT
JOIN member_booking AS mb ON mvi.mvi_id = mb.mvi_id
WHERE CURDATE() NOT BETWEEN mb.mb_startdate
AND mb.mb_returndate
AND mvi.mvi_id NOT IN (SELECT mvi.mvi_id
FROM member_booking)
But it doesn't give the results want.
To return rows from `master_vehicle_inventory` which do not have a "matching" row in `member_booking` table, we can use an anti-join pattern.
Assuming \mb_startdate` and `mb_enddate` are defined as datatype DATE.
Something like this:
SELECT mvi.mvi_id
FROM master_vehicle_inventory mvi
LEFT
JOIN member_booking mb
ON mb.mvi_id = mvi.mvi_id
AND mb.mb_startdate <= DATE(NOW())
AND mb.mb_enddate > DATE(NOW())
WHERE mb.mvi_id IS NULL
(The specification for a "matching" row is a bit vague.)
Given DATE(NOW()) returns '2016-04-25' we expect that query to return rows
mvi_id
------
1
4
(We are suspicious that the datatype of the columns may not be DATE because the values shown in the example data are not in the YYYY-MM-DD format we expect returned for a DATE expression.)

WHERE clause in SSRS expression for max function

I have for example a query with return something as it
route value
1 3
2 2
3 4
4 5
5 1
then I need to put in 2 textbox the max and the min route so in sql this would be
select top 1 route from table where value=(select max(value) from table)
I add a image done in excel, how this would be.
I believe this is so easy but I dont have idea how to get it.
I got using expression, this was extactly expression
="Route "+
Convert.ToString (
Lookup(max(fields!value.Value),fields!value.Value ,fields!route.Value,"mydataset")
)
changing max for min, for the other...
thanks everyone.
I believe the query you're looking for would be:
With Min_Max_CTE as (
Select MIN(value) as Min_Value
, MAX(value) as Max_Value
From Table
)
Select Top 1 'Min' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Min_Value
Union All
Select Top 1 'Max' as Type
, T.route
, T.value
From Table T
Inner Join Min_Max_CTE CTE
on T.value = CTE.Max_Value
Order by Type desc --This will put the Min Route first followed by the Max Route
Then, put that query into a dataset, and then create a tablix and use the Type, route, and value fields to return the minimum route and the maximum route. It should end up being set up just like your excel section with the min and max routes above.
You can do this SSRS by using a couple of separate tables. Your example data:
And two tables in the Designer:
Since the tables only have header rows, only the first row in the table will be displayed.
To make sure we get the MAX and MIN values in the two tables, each table needs to order its Dataset appropriately, i.e. by Value by descending and ascending respectively.
MAX table:
MIN table:
Which gives your expected result: