i am a beginner and need some help on this. trying to update a table with data from multiple tables and grouping them in months or weeks
update monthly_report
SET De_Ative = (SELECT IFNULL(sum(statuschange.NUM_STATUS_CHANGES),0) FROM statuschange
WHERE month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')) = monthly_report.Mon
AND statuschange.STATUS_CHANGE ='CANCEL' OR statuschange.STATUS_CHANGE ='LOCK'
group by month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')))
[SQL] update monthly_report
SET De_Ative = (SELECT IFNULL(sum(statuschange.NUM_STATUS_CHANGES),0) FROM statuschange
WHERE month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')) = monthly_report.Mon
AND statuschange.STATUS_CHANGE ='CANCEL' OR statuschange.STATUS_CHANGE ='LOCK'
group by month(STR_TO_DATE(statuschange.Evaluation_day,'%d.%m.%Y')))
-- group by monthly_report.Mon
[Err] 1242 - Subquery returns more than 1 row
Try this one,
UPDATE monthly_report a
LEFT JOIN
(
SELECT MONTH(STR_TO_DATE(a.Evaluation_day,'%d.%m.%Y')) mnt,
SUM(a.NUM_STATUS_CHANGES) totalSum
FROM statuschange a
WHERE a.STATUS_CHANGE IN ('CANCEL', 'LOCK')
GROUP BY MONTH(STR_TO_DATE(a.Evaluation_day,'%d.%m.%Y'))
) b ON b.mnt = a.mon
SET a.De_Ative = IFNULL(b.totalSum, 0)
Related
i have a database of students exam records based on year and semesters and i want to make changes to the level of all students in a particular year and semester with the following code which gave me an error.
update result
set level = ( select new_level.student
from new_level
where new_level.student = result.student
limit 1
)
and year = '2016/2017'
and semester = 2
will appreciate some help on resolving this. thank you
You missed the WHERE in the UPDATE statement.
update result
set level = ( select new_level.student
from new_level
where new_level.student = result.student
limit 1
)
where #what you missed.
year = '2016/2017'
and
semester = 2
You need a where clause and an update with join for join the student at both the involved tables
update result
inner join new_level on new_level.student = result.student
set result.level = new_level.student
where year = '2016/2017'
and semester = 2
or you can directly add the condition to the ON clause (avoinding the where word ) eg:
update result
inner join new_level on new_level.student = result.student
and year = '2016/2017'
and semester = 2
set result.level = new_level.student
I have this table:
Now if I count rows in column id_racuna that are not NULL using:
SELECT COUNT (id_racuna) FROM racuni WHERE id_racuna IS NOT NULL;
I get:
So if I use this value in an update like this:
UPDATE racuni AS r1 JOIN racuni AS r2 ON r1.id_interesa = r2.id_interesa
SET r1.id_racuna = (SELECT COUNT (r2.id_racuna) FROM r2 WHERE r2.id_racuna IS NOT NULL) +1,
r1.poslano = curdate()
WHERE r1.id_interesa = 8;
I would expect to get:
but I get an error:
Table r2 doesn't exist!
I tried to trick MySQL using JOIN to think that r1 and r2 are two different tables, but it doesn't work. I have seen this kind of trick here where it worked... What am I missing?
UPDATE:
I need to use JOIN because if I wrote just:
UPDATE racuni
SET id_racuna = (SELECT COUNT (id_racuna) FROM racuni WHERE id_racuna IS NOT NULL) +1,
poslano = curdate()
WHERE id_interesa = 8;
I would get error:
Table 'racuni' is specified twice, both as a target for 'UPDATE' and as
a separate source for data
Try with a cross join and wrap your sub query in it
UPDATE racuni AS r1
CROSS JOIN (SELECT COUNT (id_racuna) id_racunacount
FROM racuni
WHERE id_racuna IS NOT NULL) a
SET r1.id_racuna = a.id_racunacount + 1,
r1.poslano = curdate()
WHERE r1.id_interesa = 8;
How can I use a subquery from an UPDATE statement? Here is my query:
UPDATE car_availability
SET availability_status = 'GOOD'
FROM car_trip AS TRIP
WHERE car_availability.car_no = TRIP.car_no
AND NOT EXISTS (SELECT DISTINCT A.car_No
FROM car_maintenance A
WHERE A.car_no = TRIP.vehicle_id
AND start_date = TRIP.workday)
AND EXISTS (SELECT DISTINCT B.car_No
FROM car_maintenance B
WHERE B.car_no = TRIP.vehicle_id
AND end_date IS NOT NULL)
I get an error 'FROM clause in UPDATE and DELETE statements cannot contain subquery sources or joins.'
I want to update availability_status to GOOD in which car exists in car_maintenance with its start_date is equal to workday of car_trip and cars which is in car_maintenance with its end date is not null.
As #McNets said, you need to add the car_availability table to your FROM clause. Try this:
UPDATE CA
SET availability_status = 'GOOD'
FROM car_availability CA
JOIN car_trip AS TRIP ON CA.car_no = TRIP.car_no
WHERE NOT EXISTS (SELECT DISTINCT A.car_No
FROM car_maintenance A
WHERE A.car_no = TRIP.vehicle_id
AND start_date = TRIP.workday)
AND EXISTS (SELECT DISTINCT B.car_No
FROM car_maintenance B
WHERE B.car_no = TRIP.vehicle_id
AND end_date IS NOT NULL)
The following is the MS Sql server Update statement
Update
HC_TranDetails
SET
InsPayments = (SELECT IsNull(SUM(ISNULL(CreditAmount,0)),0) From HC_TranDetails TDS
Where TDS.TransactionType = 2
AND TDS.ClaimNo = TD.ClaimNo
AND TDS.LineItemNo = TD.LineItemNo
AND IsNull(TDS.InsPlanRowID,'') <> ''
AND TDS.ReverseEntry <> 1 ),
Adjustments = (SELECT IsNull(SUM(ISNULL(CreditAmount,0)),0) From HC_TranDetails TDS
Where TDS.TransactionType = 8
AND TDS.ClaimNo = TD.ClaimNo
AND TDS.LineItemNo = TD.LineItemNo
AND IsNull(TDS.InsPlanRowID,'') <> ''
AND TDS.ReverseEntry <> 1 ),
FROM
HC_TranDetails TD
Now i am trying the same kind of statement in mysql as follows
UPDATE claimdetails SET balanceAmount = (SELECT IFNULL(SUM(IFNULL(debitamount,0)) - SUM(IFNULL(creditamount,0)),0)
FROM claimdetail CD WHERE CD.claimID = CDS.claimID)
FROM ClaimDetail CDS
But it is showing as syntax Error near 'From ClaimDetail CDS' at line 4
MySQL is squeamish about updates on the same table. The easy fix is to include an extra level of subquery. The proper fix, though, is to use a join
UPDATE claimdetails join
(select claimid, IFNULL(SUM(IFNULL(debitamount,0)) - SUM(IFNULL(creditamount,0)),0) as val
from ClaimDetails
group by claimid
) agg
on claimdetails.claimid = agg.claimid
SET balanceAmount = agg.val;
You can join the table you want to update with a subquery that calculates the balance for each claimid on the other table.
By using LEFT JOIN, it will update all records on table claimdetails. A value of 0 will be updated to any non existent claimid on the subquery.
UPDATE claimdetails a
LEFT JOIN
(
SELECT claimID,
SUM(IFNULL(debitamount, 0)) - SUM(IFNULL(creditamount,0)) bal
FROM claimdetail
GROUP BY claimID
) b ON a.claimID = b.claimID
SET a.balanceAmount = IFNULL(b.bal, 0)
I have a master table called "parent" and a related table called "childs"
Now I run a query against the master table to update some values with the sum from the child table like this.
UPDATE master m SET
quantity1 = (SELECT SUM(quantity1) FROM childs c WHERE c.master_id = m.id),
quantity2 = (SELECT SUM(quantity2) FROM childs c WHERE c.master_id = m.id),
count = (SELECT COUNT(*) FROM childs c WHERE c.master_id = m.id)
WHERE master_id = 666;
Which works as expected but is not a good style because I basically make multiple SELECT querys on the same result. Is there a way to optimize that? (Making a query first and storing the values is not an option.
I tried this:
UPDATE master m SET (quantity1, quantity2, count) = (
SELECT SUM(quantity1), SUM(quantity2), COUNT(*)
FROM childs c WHERE c.master_id = m.id
) WHERE master_id = 666;
but that doesn't work.
Update: Here is the solution, thanks to everbody:
You can do something like this:
UPDATE master m
INNER JOIN childs c ON m.master_id = c.master_id
SET master.quantity1 = c.quantity1,
master.count = 1
If you have only one child record at a time. However if you want to use a group function like SUM() in the joined table that doesn't work. Either you get a "Invalid use of group function" if you leave the "group by" part or a "You have an error in your sql syntax if you use "GROUP BY c.master_id"
-- This doesnt work :(
UPDATE master m
INNER JOIN childs c ON m.master_id = c.master_id
SET master.quantity1 = SUM(c.quantity1),
master.count = COUNT(c.*)
GROUP by c.master_id
The solution is to use JOIN with a subquery:
UPDATE master m
INNER JOIN
(
SELECT master_id,
SUM(quantity1) as quantity1,
COUNT(*) as count
FROM childs c
GROUP BY master_id
) c
ON c.master_id = m.master_id
SET m.quantity1 = c.quantity1,
m.count = c.count
WHERE m.master_id = 666;
But since this pulls every row from the childtable the overhead would likely be bigger than using more subqueries like in the original sql. So you should add a WHERE clause to the joined table to get only the rows you need.
Another interesting approach is this syntax, which does the same as the JOIN with the WHERE clause but you should only use if if you want to update all rows with the same values and your subquery only returns one row, since the result from the subquery gets appended to the result and can be used like any column.
UPDATE master m,
(
SELECT SUM(c.quantity1) as sum_of_quantity,
COUNT(*) as rowcount FROM child c WHERE c.master_id = 666
) as c
SET m.quantity1 = c.sum_of_quantity,
m.count = c.rowcount
WHERE m.master_id = 666;
Rewriting Lieven's solution to MySQL:
UPDATE master m
JOIN (
SELECT master_id
, SUM(quantity1) as quantity1
, SUM(quantity2) as quantity2
, COUNT(*) as count
FROM childs c
GROUP BY
master_id
) c
ON c.master_id = m.master_id
SET
m.quantity1 = c.quantity1
,m.quantity2 = c.quantity2
,m.count = c.count
WHERE m.master_id = 666;
I don't know if it is allowed in MySQL, but SQL Server allows you to use the result of a select in an update.
UPDATE master m SET
quantity1 = c.quantity1
, quantity2 = c.quantity2
, count = c.count
FROM master m
INNER JOIN (
SELECT master_id
, quantity1 = SUM(quantity1)
, quantity2 = SUM(quantity2)
, count = COUNT(*)
FROM childs c
WHERE master_id = 666
GROUP BY
master_id
) c ON c.master_id = m.master_id
You could select your data into a temporary table, and then update using that data.
If you also want to insert "new" data in the same roundtrip, look into INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE ...
If you already are doing inserts if row doesn't exist, then that would be redundant with this example.
example:
INSERT INTO master m (id, quantity1, quantity2, count)
SELECT master_id, SUM(quantity1) q1, SUM(quantity2) q1, COUNT(*) c
FROM childs
GROUP BY master_id
ON DUPLICATE KEY UPDATE
m.quantity1 = q1,
m.quantity2 = q2,
m.count = c
NOTE! This is untested code, but I think it should be possible to backreference the select result in the UPDATE.
Syntax reference: http://dev.mysql.com/doc/refman/5.0/en/insert.html