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.
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:
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"];
My Query-
SELECT id, MAX(sl), details_sp FROM shareholderreceive GROUP BY id
and result
id MAX(sl) details_sp
1 76
2 74
3 64
4 67
5 69
6 70
10 72
But i need Those MAX(sl) Where details_sp column has last entered value.
My expected table is -
id MAX(sl) details_sp
1 72 Jul'16
2 74
3 64
4 62 Aug'16
5 69
6 70
10 71 Aug'16
here, data type details_sp is Varchar.
What query do I need to get this in MySQL?
What I think you mean is that when there is a details_sp filled in you need the sl value of that row and when there is no details_sp you need the max sl value?
If so, don't use MAX() but use the ORDER BY with the GROUP BY.
SELECT id, sl, details_sp
FROM shareholderreceive
ORDER BY details_sp DESC, sl DESC
GROUP BY id
The only problem with this is that you can never order by a date when it is a varchar value. This means you can never get the latest entry with just a varchar as a date.
So if there is a way, make the details_sp a date value (when you don't have a day just use the first day of the month, so 2017-06-01 for juli 2017) and use DATE_FORMAT() in MySQL or date_format in PHP (or JAVA or etc).
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
I've multiple values with different timestamps like the following:
10 01:01:00
20 01:35:00
30 02:10:00
05 02:45:00
12 03:05:00
21 03:30:00
10 04:06:00
40 05:15:00
I don't have a column with which I can group by and find max. I want to get the records with max values like 30,21, and 40. The data is always in this format, like value increasing and then starts from zero again. What query will help me to find these records?
To clarify, it's sorted by the timestamp, and I want to get the timestamps for the local maxima, the rows where the next row has a lesser value:
value tmstmp
----- --------
10 01:01:00
20 01:35:00
30 02:10:00 <-- this one since next value is 5 (< 30).
05 02:45:00
12 03:05:00
21 03:30:00 <-- this one since next value is 10 (< 21).
10 04:06:00
40 05:15:00 <-- this one since next value is 40 (< infinity).
Somehow your question is not clear to me.
Assume that first column name is "value" and second column name is "timestamp".
Select Max(value) from group by timestamp.
This answer might be a bit late, however i think i have found the solution
SELECT * FROM temp t1 WHERE value >
IFNULL(
(SELECT value FROM temp t2
WHERE t2.tmstmp > t1.tmstmp ORDER BY t2.tmstmp ASC limit 1),
-1
)
ORDER BY tmstmp ASC
To clarify:
I find the values where the value is greater than the next value in the row.
To also get the final value I have added an IFNULL around the subquery to make sure the subquery will then return -1
The only problem i see is when the time goes over to the next day, that's why i hope you can have a date appended to it as well.
Hopefully this will still help others