I'm trying to sum the max values of a column named value1 grouped by sensor id value so I want the last greater value from each sensor id.
Using this code I get a column with rows with the max results for each sensor. But I dont know how to sum this values.
select max(value1) from `digiwork_esp-data`.SensorData group by sensor
I need to sum this individual values of the result into a total. I tried this, but it is returning a big number (not correct):
select sum(value1)
from `digiwork_esp-data`.SensorData
where value1 = any (SELECT max(value1) FROM `digiwork_esp-data`.SensorData group by sensor)
You can directly sum the result of the subquery.
select sum(t1.val) from
(select max(value1) val FROM digiwork_esp-data.SensorData group by sensor) t1
Related
There is a table and group the records using key: stu_class|stu_birth|stu_major. If there are duplicate records, the record with the smallest stu_id is selected. So, I need to count the total number of records with satisfied this condition.
Example:
Here, stu_id (100,101) are duplicate records based on the key. But I want to select only the smallest stu_id record. It is stu_id , 100. Simillary, stu_id (102,104) are duplicate records. but need to select stu_id 102.
Then selected record count should be 2. How can I get this count using SQL?. I mean how I can get calculated total number of records as 2.
One method uses window functions:
select t.*
from (select t.*,
row_number() over (partition by stu_class, stu_birth, stu_major order by stu_id) as seqnum
from t
) t
where seqnum = 1;
This is available in MySQL starting with version 8.
An alternative uses a correlated subquery and might be faster, even in version 8:
select t.*
from t
where t.stu_id = (select min(t2.stu_id)
from t t2
where t2.stu_class = t.stu_class and t2.stu_birth = t.stu_birth and t2.stu_major = t.stu_major
);
This can take advantage of an index on (stu_class, stu_birth, stu_major, stu_id).
EDIT
If you just want the total records, then use aggregation:
select stu_class, stu_birth, stu_major, min(stu_id), count(*) as cnt
from t
group by stu_class, stu_birth, stu_major;
I have table like this
enter image description here
I need to get the data only whose age > 10, along with that i need to get the total number of records present in the table. ie. in this example it is 4 records. what i need is in single query i need to get the total number of records present in table and columns which i query.
Query will be somewhat like
SELECT ID, NAME, count(TOTAL NUMBER OF RECORDS IN TABLE) as Count from MYTABLE WHERE AGE > 10
Any idea about this ?
You can use a subquery in the FROM clause:
SELECT ID, NAME, c.cnt as Count
FROM MYTABLE CROSS JOIN
(SELECT COUNT(*) as cnt FROM MYTABLE) c
WHERE AGE > 10 ;
Both databases support window functions, but they are not really helpful here, because the count is not filtered in the same way as the outer query. If you do want the filter for both, then in the most recent versions you can do:
SELECT ID, NAME, COUNT(*) OVER () as cnt
FROM MYTABLE
WHERE AGE > 10 ;
You can try below - using scalar subquery
SELECT ID, NAME, age,(select count(*) from mytable WHERE AGE > 10) as Count
from MYTABLE
WHERE AGE > 10
I have a table name agents_commission have columns id, agent_id, deal_id, commission, deal_date.
I want to select agent_id, commission from agents_commission where month(date_deal) = '$month' and year(date_deal) ='$year';
but if the table have some rows have the same agent_id join the same rows in one row and calculate the commission values like row 66666.7 + 100000 as in row 1, 2
to be the final fetch_assoc as
array('agent_id'=>1, 'commision'=>166666.7);
thanks
You can not select all when you want to group by only one column(agentid). you can do something like below
select agent_id, sum(commission) as commission from temp where MONTH(deal_date) = '09' and year(deal_date) ='2018' group by agent_id;
The SELECT statement used in the GROUP BY clause can only be used contain column names, aggregate functions, constants and expressions.
Refer https://dev.mysql.com/doc/refman/5.5/en/group-by-modifiers.html
Try using group by and aggregation:
select agentid, sum(commission) as commission
from agents_commission
where month(date_deal) = '$month' and year(date_deal) ='$year';
group by agentid
As the title states, I want to count the amount of rows in a column using a SQL query in PHP
If you just want the number of rows, you can use the count(*) function:
SELECT COUNT(*) FROM my_table
If you want the number of values (i.e., excluding nulls), you can use count on the column:
SELECT COUNT(my_column) FROM my_table
If you want the number of different values, you can add the distinct keyword:
SELECT COUNT(DISTINCT my_column) FROM my_table
I want to select some entries based on a max+sum condition.
mytable
----------
id | col1 | col2
I want to select all entries that have the sum of col1 & col2 greater than or equal to the max of sum minus X. (don't ask me why :) )
So far I managed to get the sum OK (hereafter aliased as "total") with:
SELECT id,SUM(col1 + col2) AS total FROM mytable GROUP BY id;
I also managed to get the MAX of the sum OK (with a ORDER BY/LIMIT workaround though):
SELECT id,SUM(col + col) as total FROM mytable GROUP BY id ORDER BY total DESC LIMIT 1;
However everytime I try to re-use my alias as a condition (e.g. WHERE total >= ...) I get an "Unknown column" error
Anything would be greatly appreciated
You have some misconceptions about SUM. SUM is an aggregating function, means it works on many records and not just one.
To calculate the sum of two fields per record, you should use only the + operator.
SELECT id, col1+col2 AS 'total'
FROM T1
WHERE
(col1+col2+x) >=(SELECT MAX(col1+col2) from T1)
If you are using group by, you'll need to use a having clause:
SELECT id,SUM(col1+col2) as total FROM mytable GROUP BY id ORDER BY total HAVING total >= x