This question already has answers here:
What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?
(9 answers)
Closed 6 years ago.
here's my recordset:
id date
-----------------------
1 2017-01-11
2 2017-01-12
3 2017-01-14
4 2017-01-15
4 2017-01-16
i'd like to query all records within the date range 2017-01-14 to 2017-01-16
currently i'm using:
SELECT * FROM foo WHERE (date='2017-01-14' OR date='2017-01-15' OR date='2017-01-16')
is there a better way (which would be probably faster for bigger ranges)?
thanks
PS: i'm aware i could use:
SELECT * FROM foo WHERE date >= '2017-01-14' AND date <= '2017-01-16'
but the problem is that i don't want "gaps" between each day.
SELECT *
FROM foo
WHERE date BETWEEN '2017-01-14' AND '2017-01-16';
Related
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
MySQL - Fetching lowest value
(7 answers)
Closed 8 months ago.
first sorry for the silly question but I'm not an expert using SQL. I've done some search about this, but none of the answers were working for me.
I have a table with this structure:
sensor_id
temp_value
temp_time
TEMP_1
19
00:00:07
TEMP_1
13
01:00:00
TEMP_1
11
03:10:10
TEMP_1
15
07:33:49
TEMP_2
25
00:00:07
TEMP_2
20
01:00:00
TEMP_2
22
03:10:10
TEMP_2
28
07:33:49
I would like to get the minimum value for each sensor_id but with the corresponding time.
Here is the query I do:
SELECT MIN(temp_value) as value,sensor_id,temp_time FROM nd_sensor_value
WHERE sensor_id = "TEMP_1" OR sensor_id = "TEMP_2" GROUP BY sensor_id
(I've added the sensor_id in WHERE because I have lot of other sensors in the table but I want just these 2)
With this query I get this result:
sensor_id
temp_value
temp_time
TEMP_1
11
00:00:07
TEMP_2
20
00:00:07
The values are okay, but the time isn't. It takes the min value but with the time of the first record.
This is the result I expect:
sensor_id
temp_value
temp_time
TEMP_1
11
03:10:10
TEMP_2
20
01:00:00
I'm using MySQL with PHP, it is for a side project.
I know that there is lots of way to do it, but my table contains a huge amount of records and I'm looking for the more optimal query.
Is Anyone able to help me with this ?
Many thanks !
This question already has answers here:
MySQL Select minimum/maximum among two (or more) given values
(4 answers)
Closed 2 years ago.
MySQL version 8.0
Given a table:
id date1 date2
1 2020-01-01 2020-01-02
2 2020-03-03 2020-05-02
3 2020-04-04 2020-02-11
I would like to create additional column that chooses min(date1, date2).
I know min(date1) = column-wise earliest date but how can I do row-wise minimum operation on dates?
Desired output should look like this:
id date1 date2 min(date)
1 2020-01-01 2020-01-02 2020-01-01
2 2020-03-03 2020-05-02 2020-03-03
3 2020-04-04 2020-02-11 2020-02-01
Thanks in advance!
Answer:
MySQL Select minimum/maximum among two (or more) given values
Simply use least function -> id, date1, date2, least(date1,date2)
I had to search for a while to find the answer so I am hoping this would lead to answer quicker.
This question already has answers here:
Simple way to calculate median with MySQL
(48 answers)
Closed 4 years ago.
Could anybody give me a hint how to find median value for "tax" during 12.04.18 - 16.04.18:
user_id login_time tax
3 2018-04-15 16625000
5 2018-04-16
6 2018-04-17 296470000
6 2018-04-16 192519750
6 2018-04-15 4455500
6 2018-04-13 17125
6 2018-04-12 120180000
7 2018-04-18 24060000
7 2018-04-17 42959500
The result equals 16625000 (because there is NULL value. We need to use it as 0).
Thank U for attention to my question!
The median is the value
located exactly in the middle of an odd dataset.
Or the average of the two middle values in an even dataset.
So, by considering this two cases, the first you need is the count of datarows. Then, you have to decide (simple case) if you pick the value in the middle, or if you need the average of two values (Don't forget to apply sorting prior to selecting the actual values):
I would use a little "code" to achieve this:
Pseudo-Code:
1.) SELECT count(id) AS val FROM myTable WHERE datetime ... //$val=9
2.) Programming language: $lim = floor($val/2); // $lim=4
if odd($val){
3.) SELECT tax FROM myTable WHERE datetime [...] ORDER BY tax LIMIT $lim,1
}
else if even($val){
3.) Programming language: $lim -=1; // if $val was 10, we want row 4 and 5
4.) SELECT AVG(tax) AS tax FROM
(SELECT * FROM myTable WHERE datetime [...] ORDER BY tax LIMIT $lim,2) AS tmp
}
[...]
echo "Median is: ". $row["tax"];
This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 7 years ago.
I seem to remember this working as expected in Oracle, but I can't figure out how to get it right in MySQL.
Here is my query:
SELECT DateTimeStamp, MAX(Value) FROM t1
GROUP BY YEAR(DateTimeStamp), MONTH(DateTimeStamp), DAY(DateTimeStamp);
Running this produces results like:
DateTimeStamp Value
2015-09-09 00:00:29 100
2015-09-10 00:00:05 58
2015-09-11 00:00:57 62
2015-09-12 00:00:49 69
2015-09-13 00:00:43 97
But I was expecting it to look like this, where the DateTimeStamps match up with the values:
DateTimeStamp Value
2015-09-09 03:28:29 100
2015-09-10 03:29:05 58
2015-09-11 03:31:57 62
2015-09-12 03:30:49 69
2015-09-13 03:28:43 97
The correct maximum values are being selected, but the matching DateTimeStamps for those maximum values are not. Instead, it looks like the first DateTimeStamp value for each day is being selected. How can I change my query to display the matching DateTimeStamps?
If all you need is to strip the time section, you need to use the date function:
SELECT date(DateTimeStamp) AS DateTimeStamp, MAX(Value) AS MaxValue
FROM t1
GROUP BY date(DateTimeStamp);
Note: Grouping by something usually make sense when you select it. There is no point to group by YEAR(DateTimeStamp), MONTH(DateTimeStamp), DAY(DateTimeStamp) if all you need is the maximum value per each day.
This question already has an answer here:
Mysql query to select a distinct column and the count of a value in another column where column like distinct coumn?
(1 answer)
Closed 9 years ago.
My data table is as below:
ID WEEK RESULT
1 13 GOOD
2 13 BAD
3 13 GOOD
4 13 WORST
5 14 GOOD
6 14 BAD
7 14 WORST
8 15 BAD
9 15 WORST
I need a sql query to create an array as below:
WWEK GOOD_RESULT BAD_RESULT WORST_RESULT TOTAL
13 2 1 1 4
14 1 1 1 3
15 0 1 1 2
Can anyone please help me to find an appropriate mysql query?
SELECT
WEEK,
SUM(RESULT='GOOD') As GOOD_RESULT,
SUM(RESULT='BAD') As BAD_RESULT,
SUM(RESULT='WORST') AS WORST_RESULT,
COUNT(*) As TOTAL
FROM YourTable
GROUP BY
WEEK
Please see fiddle here.