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.
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:
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';
This question already has answers here:
MySQL how to fill missing dates in range?
(6 answers)
Closed 7 years ago.
I have mysql query
SELECT date, COALESCE(SUM(events),0) AS hotel_c
FROM event_cohort_report
WHERE date >= '2015-08-05' AND campaign='Pointific_Incent' AND country='IN'
GROUP BY date
when i run this query i get this result
date hotel_c
2015-08-07 5411
2015-08-08 4602
2015-08-09 5151
2015-08-10 183
2015-08-11 1
But i want for all date sum with zero if its not present in the table something like this.
date hotel_c
2015-08-05 0
2015-08-06 0
2015-08-07 5411
2015-08-08 4602
2015-08-09 5151
2015-08-10 183
2015-08-11 1
The code that you are using is correct for getting value for null records. But I don't know the table and records in that as you have not mentioned. I have a working modal on this link
You can also use IFNULL(SUM(column),0) for replacing null with 0
This question already has answers here:
Mysql: Select all data between two dates
(7 answers)
Closed 7 years ago.
I have a two column with date (from, to) in date format (Y-m-d), and I need MySQL SELECT which returns a correct value:
eg. I have same events:
1. 2015-07-10 to: 2015-07-25
2. 2015-07-21 to: 2015-07-24
3. 2015-07-19 to: 2015-08-01
4. 2015-07-29 to: 2015-08-05
5. 2015-08-05 to: 2015-08-06
6. 2015-08-10 to: 2015-08-10
and I need all events which is between 2015-07-11 and 2015-07-21.
**expected results:**
*1,2,3*
Thank you!
For ID 2, ID 3 you can check with "Between". For ID 1 you have to check if:
Is From lower/equal than my start point?
Is To higher/equal than my end point?
With this two questions you ensure it is within your Range.
http://sqlfiddle.com/#!9/642fb7/1
Here you got an example. :)
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.