Im trying to write a formula in SQL Server to subtract the values of two columns which are [AmountSpent]column of table2 and [Amount] column of table1of two different tables and update the balance amount in [Amount] column any idea ?
Thank you in advance...
I think the following will work, assuming you have a reliable foreign key relationship between the two tables
UPDATE [table1]
SET [table1].[Amount] = [table1].[Amount] - [table2].[AmountSpent]
FROM
[table1] INNER JOIN
[table2] ON
[table1].[KeyField] = [table2].[KeyField]
i got it.........thanks #james Osborn
create procedure SP_Subtraction
(
#EmpID int
)
as
begin
UPDATE PTS_BalanceTracker
SET PTS_BalanceTracker.Balance_BalanceAmount = PTS_BalanceTracker.Balance_BalanceAmount - PTS_Transactions.Transaction_Amount
FROM
PTS_BalanceTracker INNER JOIN
PTS_Transactions ON
PTS_BalanceTracker.Emp_ID = #EmpID
end
Related
I have a temporary table havinf 2 colums and i need to update this data into another table
Can Some one please help me with this
You want to use join, something like this:
update othertable t join
temporarytable tt
on t.clientid = tt.clientid
set t.checklist = tt.checklist;
I am trying to get a minimum value from the Candidate table and insert that value in the MinTotal table. Can you do both in one SQL statement?
Here's my SQL Statement:
UPDATE MinTotal SET MinTotal.min_total= MIN(CandidateID.TotalVotes);
UPDATE MinTotal a
INNER JOIN (SELECT MIN(c.TotalVotes) min_vote, c.CandidateID FROM Candidate c
GROUP BY c.CandidateID) b ON b.CandidateID = a.CandidateID
SET a.min_total = b.min_vote;
Try the above. This is specific for each candidate, else you can use the other answers provided.
You have to use a select so you can properly set your MIN().
One way of doing that would be like that:
UPDATE MinTotal
SET
min_total = Cmin.minresult
FROM (
SELECT MIN(TotalVotes) as minresult
from CandidateID
) Cmin
In general, that would be one way to solve the Problem. In this case you would set the minresult for every row you have in your MinTotal table. If you dont want that, you may need to be more specific about your desired output and add some examples in your question
UPDATE MinTotal
SET MinTotal.min_total = (
SELECT MIN(TotalVotes)
FROM CandidateID
);
The aim of my query is to use a value from one table A to look up a corresponding value from another table B, and then use that value to look up another value from another table C. This value should then be used to update a column in the original table A.
In my query so far I have managed to get a correct list of the values from table C using this command. In this command, Disbursements, Employee_Details & List_State_Codes correspond to tables A, B & C respectively as described above.
SELECT `List_State_Codes`.`Code`
FROM `List_State_Codes`
LEFT JOIN (
`Employee_Details` , `Disbursements`
) ON ( `Employee_Details`.`STATE` = `List_State_Codes`.`STATE` )
WHERE `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
This returns the correct list of values as required: One value from table C for each row in table A. Now my issue is to update the required column from table A with these returned values. This is where I am stuck.
The following query is what I believe to be my closest attempt:
UPDATE `Disbursements`
SET `Disbursements`.`CostCentreID` =
(
SELECT `List_State_Codes`.`Code`
FROM (SELECT * FROM `List_State_Codes`) AS `table`
LEFT JOIN (
`Employee_Details` , `Disbursements`
) ON ( `Employee_Details`.`STATE` = `List_State_Codes`.`STATE` )
WHERE `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
)
I receive the
error #1093 - You can't specify target table 'Disbursements' for update in FROM clause, despite adding the FROM (SELECT * FROM List_State_Codes) AS table line.
Thanks for any help.
You can join in the update, and this can help to remove the offending table from the subquery:
UPDATE `Disbursements`
JOIN `Employee_Details`
ON `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
LEFT JOIN `List_State_Codes`
ON `Employee_Details`.`STATE` = `List_State_Codes`.`STATE`
SET `Disbursements`.`CostCentreID` = `List_State_Codes`.`Code`
However, I could not test this, so let me know if this helps. (or if some other error occurs)
Thanks to Paul's help I have found the correct solution to my problem. The second join was what I needed as Paul suggested, along with an additional table alias to prevent the classic #Error 1903. I also needed to add the List_State_Codes (table C) to the JOIN to ensure it could access the correct data.
UPDATE `Disbursements`
JOIN `Employee_Details`
ON `Employee_Details`.`EmployeeID` = `Disbursements`.`EmployeeID`
LEFT JOIN (
`Employee_Details` d,
`List_State_Codes`
) ON
`Employee_Details`.`STATE` = `List_State_Codes`.`STATE`
SET `Disbursements`.`CostCentreID` = `List_State_Codes`.`Code`
I have two table in my SQL Database
One is Companies & Another is Company_advertisements.
I want to update field of company table "adv"
SET Company.adv = 1 if Company_adv.image is not empty
SET Company.adv = 0 if Company_adv.image is empty
The foreign key for Company_adv table is company_id
I tried using the following syntax but it didn't worked.Instead after running this query all the values of Companmy.adv become 1 after that
I tried this query:
UPDATE companies cmp, company_adv cma
SET cmp.adv=1
WHERE cma.company_id=cmp.id AND
cma.image1 IS NOT NULL
Please help me !!
if that's the case, you need to use LEFT JOIN
UPDATE companies cmp
LEFT JOIN company_adv cma
ON cmp.id = cma.company_id
SET cmp.adv = IF(cma.image1 IS NULL, 0, 1)
You could try:
UPDATE companies
SET adv=IF((SELECT image
FROM company_adv
WHERE companies.id=company_adv.company_id) IS NULL,
0,
1);
I have the database of ATM card in which there are fields account_no,card_no,is_blocked,is_activated,issue_date
Fields account number and card numbers are not unique as old card will be expired and marked as is_block=Y and another record with same card number ,account number will be inserted into new row with is_blocked=N . Now i need to update is_blocked/is_activated with help of issue_date i.e
UPDATE card_info set is_blocked='Y' where card_no='6396163270002509'
AND opening_date=(SELECT MAX(opening_date) FROM card_info WHERE card_no='6396163270002509')
but is doesn't allow me to do so
it throws following error
1093 - You can't specify target table 'card_info' for update in FROM clause
Try this instead:
UPDATE card_info ci
INNER JOIN
(
SELECT card_no, MAX(opening_date) MaxOpeningDate
FROM card_info
GROUP BY card_no
) cm ON ci.card_no = cm.card_no AND ci.opening_date = cm.MaxOpeningDate
SET ci.is_blocked='Y'
WHERE ci.card_no = '6396163270002509'
That's one of those stupid limitations of the MySQL parser. The usual way to solve this is to use a JOIN query as Mahmoud has shown.
The (at least to me) surprising part is that it really seems a parser problem, not a problem of the engine itself because if you wrap the sub-select into a derived table, this does work:
UPDATE card_info
SET is_blocked='Y'
WHERE card_no = '6396163270002509'
AND opening_date = ( select max_date
from (
SELECT MAX(opening_date) as_max_date
FROM card_info
WHERE card_no='6396163270002509') t
)