MYSQL - "#1111 - Invalid use of group function" error - mysql

I'm programming a weather station for a school project.
In mysql I have 1 table with my readings and another one with calculated values. I wrote a mysql query to update the second table with the calculated values. When I run this query, I receive this error
1111 - Invalid use of group function
I don't know what I'm doing wrong.
My query:
UPDATE Waarnemingen2 As t1
INNER JOIN (SELECT `Datum_Tijd`,`Temperatuur`,`Luchtvochtigheid`,`Luchtdruk`,`Regenhoeveelheid` FROM Waarnemingen GROUP BY day(`Datum_Tijd`) + hour(`Datum_Tijd`)) as t2
SET t1.`Min. temperatuur` = MIN(`Temperatuur`),
t1.`Gem. temperatuur` = AVG(`Temperatuur`),
t1.`Max. temperatuur` = MAX(`Temperatuur`),
t1.`Min. luchtvochtigheid` = MIN(`Luchtvochtigheid`),
t1.`Gem. luchtvochtigheid` = AVG(`Luchtvochtigheid`),
t1.`Max. luchtvochtigheid` = MAX(`Luchtvochtigheid`),
t1.`Min. luchtdruk` = MIN(`Luchtdruk`),
t1.`Gem. luchtdruk` = AVG(`Luchtdruk`),
t1.`Max. luchtdruk` = MAX(`Luchtdruk`),
t1.`Regen` = SUM(`Regenhoeveelheid`)
The query should take the minimum, maximum and average from the columns "Temperatuur", "Luchtvochtigheid" and "Luchtdruk" from each hour.
Is there someone who can help me?

Your error is because you have unaggregated columns in a SELECT statement that are not in your GROUP by clause.
In a SELECT with a GROUP BY, the only columns you can select are:
aggregations (MAX(Temperatur), MIN(Luchtdruk), etc.)
grouped columns
Otherwise you get this error.
Change your subquery to perform your aggregations and then just assign those aggregated values to your joined table.
(I don't know MySQL syntax, this code won't work in SQL Server because it does UPDATE JOINs a little differently, but the important concept is the aggregation.)
i.e.
UPDATE Waarnemingen2 As t1
INNER JOIN (
SELECT day(`Datum_Tijd`) as day, hour(`Datum_Tijd`) as hour, MIN(Temperatur) as Min_Temperature FROM Waarnemingen GROUP BY day(`Datum_Tijd`) , hour(`Datum_Tijd`)
) t2 on t1.day = t2.day and t1.hour = t2.hour
set
t1.MinTemp = t2.Mintemp

Related

SQL subquery with reference to parent table gives "Unknown column 'test_results.id' in 'where clause'"

First off, take a look at diagram (this is an application for testing students knowledge)
I already have working application, which calculates score (in percents), but to sort by score, it is required to select all the records (of current test). And it drastically slows down app (~ 10 seconds of waiting). So I decided to move that logic into single sql query.
Now, my SQL query looks like this:
select test_results.*,
(
select test_result_total_score * 100 / test_result_total_max_score
from (
select (select sum(question_score)
from (
select question_total_right_answers = question_total_options as question_score
from (
select (
select count(*)
from answers
inner join answer_options on answer_options.id = answers.answer_option_id
where answers.asked_question_id = asked_questions.id
and answers.is_chosen = answer_options.is_right
) as question_total_right_answers,
(
select count(*)
from answers
left join answer_options on answer_options.id = answers.answer_option_id
where answers.asked_question_id = asked_questions.id
) as question_total_options
from asked_questions
where asked_questions.test_result_id = test_results.id
) as rigt_per_question
) as questions_scores) as test_result_total_score,
(select count(*)
from asked_questions
where asked_questions.test_result_id = test_results.id) as test_result_total_max_score
) as right_per_test_result
) as result_in_percents
from test_results
where test_results.id between 1 and 200;
Here is what it should do: for each asked question collect how many answer options there are (question_total_options) and how many answers user selected right (question_total_right_answers) - the very nested subqueries.
Then for each of this results calculate score (this is basically 1 if user selected all right options and 0 if at least one option is selected not right).
After that, we sum scores of all that questions (test_result_total_score - how many questions user answered right). Also, we calculate how many questions there are in test result (test_result_total_max_score).
With that information we can calculate percentage of right answered questions (test_result_total_score * 100 / test_result_total_max_score)
And the error is on lines 23 and 28:
where asked_questions.test_result_id = test_results.id
where asked_questions.test_result_id = test_results.id) as test_result_total_max_score
It says: [42S22][1054] Unknown column 'test_results.id' in 'where clause'
I have tried using variable #test_result_id like this:
select test_results.*,
#test_result_id := test_results.id,
( ... )
where asked_questions.test_result_id = #test_result_id
where asked_questions.test_result_id = #test_result_id) as test_result_total_max_score
And it evaluates, but in wrong way (probably because order of evaluation select values is undefined). BTW, all result_in_percents correspond to very first result.
For those facing similar problem, it seems that there is no simple solution.
First off, you can try rewrite your subqueries with joins as I did (see below). But when you would like to perform group operations on grouped results, you are really unhappy person). A "dirty" solution might be create function to overcome barrier of nesting subqueries.
create function test_result_in_percents(test_result_id bigint unsigned)
returns float
begin
return (
select sum(tmp.question_right) * 100 / count(*)
from (select sum(answers.is_chosen = answer_options.is_right) = count(*) as question_right
, asked_questions.test_result_id as test_result_id
from answers
inner join answer_options on answer_options.id = answers.answer_option_id
inner join asked_questions on asked_questions.id = answers.asked_question_id
where asked_questions.test_result_id = test_result_id
group by answers.asked_question_id
) as tmp
group by test_result_id
);
end;
And then, just use this function:
select (test_result_in_percents(test_results.id)) as `result_percents`
from `test_results`
where `test_results`.`test_id` = 181
and `test_results`.`test_id` is not null
order by `test_results`.`id` desc;

Need assistance with SQL Query with error message Operand should contain 1 column(s)

Good Day
I have the following query but I'm getting an error message 'Operand should contain 1 column(s)'
Any assistance would be greatly appreciated
UPDATE expenditure
SET BP = (
SELECT * ,
SUM(balance_provision - actual_amt_voucher) over (partition by voteid order by expenditureid) AS BalanceProvision
FROM expenditure
)
It looks like you want to update column bp with a window sum.
Your query fails because you are trying to assign a resultset (that has multiple columns) to a single column.
But even you were returning a scalar value from the subquery, this would not work, since MySQL does not allow reusing the target table of the update in a subquery.
Instead, yo can use the update ... join syntax. Assuming that expenditureid is the primary key of the table, as its name suggests, that would be:
update expenditure e
inner join (
select
expenditureid,
sum(balance_provision - actual_amt_voucher) over (partition by voteid order by expenditureid) bp
from expenditure
group by expenditureid
) e1 on e.expenditureid = e1.expenditureid
set e.bp = e1.bp

How to update columns in another table from a select query?

i want to update some columns in another table from a query result. I keep getting error. Please help.
Update customer_info
set customer_info.reader_ID = aisle_info.reader_ID,
customer_info.tag_no = tag_logs.tag_no,
customer_info.area = aisle_info.area,
customer_info.max_timestamp = TIMESTAMPDIFF(SECOND,MIN(tag_logs.timestamp),MAX(tag_logs.timestamp))
FROM tag_logs
INNER join aisle_info ON tag_logs.reader_ID = aisle_info.reader_ID
WHERE T.tag_no = 515988190124;
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM tag_logs
INNER join aisle_info ON tag_logs.reader_ID = aisle_info.reader_I' at line 5
You were close. Different databases have slightly different syntax on updates from select/join. Think of MySQL as a select statement, and use the alias of the primary table you are trying to update and then the SET clauses are after that.
So I first will start by writing the SELECT query by itself.
select
CI.Tag_No,
AI.Reader_ID,
AI.Area,
MIN( TL.TimeStamp ) MinTime,
MAX( TL.TimeStamp ) MaxTime
from
customer_info CI
join tag_logs TL
CI.tag_no = TL.tag_no
join aisle_info AI
on TL.Reader_ID = Reader_ID
WHERE
CI.tag_no = 515988190124
group by
CI.Tag_No,
AI.Reader_ID,
AI.Area
So this gives us the final results of what you want, and you can confirm it as needed.
Then apply your update such as
update Customer_Info CIUpd
JOIN
( select
CI.Tag_No,
AI.Reader_ID,
AI.Area,
MIN( TL.TimeStamp ) MinTime,
MAX( TL.TimeStamp ) MaxTime
from
customer_info CI
join tag_logs TL
CI.tag_no = TL.tag_no
join aisle_info AI
on TL.Reader_ID = Reader_ID
WHERE
-- notice your filter is HERE for the one tag_no you want to update
-- and will result with only this on TAG_NO set of values returned
CI.tag_no = 515988190124
group by
CI.Tag_No,
AI.Reader_ID,
AI.Area ) FirstQuery
-- the JOIN will ensure updating only on that one tag_no
ON CIUpd.Tag_No = FirstQuery.Tag_No
set
CIUpd.Reader_ID = FirstQuery.Reader_ID,
CIUpd.Area = FirstQuery.Area,
CIUpd.Max_TimeStamp = TimeStampDiff( second, FirstQuery.MinTime, FirstQuery.MaxTime )
Now this is not a perfect answer as your original query was not a proper query using the MIN() / MAX() context. When doing an aggregate query, you need to apply a group by on all NON aggregate columns. In this case you did not clarify any such group consideration on the Reader_ID and Area which COULD result in multiple rows from your tag_logs and aisle_info tables.
If the aisle info will be the same all the time for a given Tag_No, then that's simple, just skip the grouping on that and change those column retrieve values as MAX() each. if they never change, MAX() or even MIN() will always return the same value and not have an issue with the aggregate query without non-aggregate columns.
If you can provide additional clarification of data, purpose, etc, please edit your original post vs just leaving a comment. Then leave a comment for me to please review with updated info.

Should MAX in Group By Retrieve the Whole Row Data

I have a bunch of sql columns to retrieve. This is my SQL statement:
SELECT b.iposx_model_id,
a.mrModel, MAX(a.mrRevision) as mrRevision, a.mrApprovedBy, ...
FROM mydb1.tbl_model_revision a
INNER JOIN mydb2.model_from_revision_process b
ON b.mrModel = a.mrModel
WHERE a.mrType = 1
GROUP BY a.mrModel
ORDER BY b.iposx_model_id ASC
On a certain mrModel column taht I retrieved, these are the data that I'm querying for:
In my query, it states that I should get the data with the Max mrRevision which corresponds to the third row.
However, upon running the sql statement, I got the max mrRevision which is 2, coming from the third row. But the other column data I get came from the 2nd row, an example is mrApprovedBy which is 1035 instead of 10. Why is that happening?
The use group by for only a part of the column not involved in aggegation function is select clause
is depreacted in SQL because where work produce unpredicatble result
In most DB and in the most recent version of mysql this use of group by is not allowed
for obtain the row corresponding to the max value you could use a join on the values that match the right content
SELECT
b.iposx_model_id
, a.mrModel
, t.mrRevision
, a.mrApprovedBy
, ...
FROM mydb1.tbl_model_revision a
JOIN (
select mrModel, MAX(mrRevision) as mrRevision
from mydb1.tbl_model_revision
GROUP BY a.mrModel
) t on a.mrModel = t.mrModel and a.mrRevision = t.mrRevision
JOIN mydb2.model_from_revision_process b ON b.mrModel = a.mrModel
WHERE a.mrType = 1
ORDER BY b.iposx_model_id ASC

mySQL update a value

Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks
In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)