Grouping results using MIN operator [duplicate] - mysql

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 !

Related

MySQL query several records from certain date range [duplicate]

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';

Missing values on count in mysql

I'm just stuck with this issue atm and I'm not 100% sure how to deal with it.
I have a table where I'm aggregating data on week
select week(create_date),count(*)
from user
where create_date > '2015-02-01'
and id_customer between 9 and 17
group by week(create_date);
the results that I'm getting have missing values in the count, as shown below
5 334
6 376
7 394
8 405
9 504
10 569
11 709
12 679
13 802
14 936
15 1081
16 559
21 1
24 9
25 22
26 1
32 3
34 1
35 1
For example here from 16 to 21 there a obviously 4 values missing I would like these values to be included and count to be 0. I want this because I want the weeks to be matching with other metrics as we are outputting them in an excel file for internal analysis.
Any help would be greatly appreciated.
The problem is that an sql query cannot really produce data that is not there at all.
You have 3 options:
If you have data for each week in your entire table for the period you are querying, then you can use a self join to get the missing weeks:
select week(t1.create_date), count(t2.id_customer)
from customer t1
left join customer t2 on t1.id_customer=t2.id_customer and t1.create_date=t2.create_date and t2.id_customer between 9 and 17
where t1.create_date > '2015-02-01'
group by week(t1.create_date)
If you have missing weeks from the customer table as whole, then create a helper table that contain week numbers from 1 or 0 (depending on mysql config) to 53 and do a left join to this helper table.
Use a stored procedure that loops through the results of your original query and inserts the missing data in the resultset using a temporary table and then returns the extended dataset as result.
The problem is that there is no data matching your criteria for the missing weeks. A solution will be to join from a table that has all week numbers. For example if you create a table weeknumbers with one field weeknumber containing all the numbers from 0 to 53 you can use something like this
select weeknumber,count(user.*)
from weeknumbers left join user on (weeknumbers.weeknumber=week(user.create_date)
and user.create_date > '2015-02-01'
and user.id_customer between 9 and 17)
group by weeknumber;
Additionaly you might want to limit the week numbers you do not want to see.
The other way is to do it in the application.

MySQL GROUP BY DateTime with Aggregate Function [duplicate]

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.

How to get sum null values as zero while using group by [duplicate]

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

MySQL query to convert a table into distinct column and other column count array? [duplicate]

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.