Like
tablename:db
Fname Salary
AB 500
CD 1000
How to copy 1000 to AB that Salary
Not hard coding the amount
If you are looking for updating salary of AB with salary of CD, you can try this below Update script-
UPDATE your_table_name
SET Salary = (SELECT Salary FROM your_table_name WHERE Fname = 'CD')
WHERE Fname = 'AB'
You have to make sure there is one single row for "CD" in your table. If there multiple available, some error will raised.
This is just a sample Update script you can check the logic how you can update a column's value with another row's value. You can now adjust the query for your purpose accordingly.
Remember, never to play with your real data using Update/Delete script. Try up on your test data first.
Related
I would like to do if this column = 'Johanna' then the value in column salary should be divided by 100 in SAS.
What is the best approach to do that in SAS
Thank you
Mr.F
SQL update can modify a value in a table with out rebuilding (rewriting) the entire table. A DATA Step will rewrite an output data set.
Proc SQL;
* divide a persons salary by a constant;
update mytable
set salary = salary / 100
where name = 'Joanna'
;
Repeated submits of the code will divide the salary each time, so be aware of that.
update mytable
set salary = salary / (select salary from mytable where name = 'OtherValue')
where name = 'Joanna'
;
The selection criteria for the denominator (where name = 'OtherValue') can be any valid selection for the table that returns a single row.
If you want to divide a value by a value in a next or prior row make a new question and provide some context and sample data.
Assuming you’re using a data step
If columnName = 'Joanna' then salary = salary/100;
I have table AddClient
Id cId name amount
10L CL-J Jon 5000
10L CL-B Ben 4000
10L CL-J Jon 6000
10L CL-B Ben 8000
10L CL-T Tony 9000
10L CL-J Jon 5000
When I try to delete the record like this
DELETE from AddClient where cId = 'CL-J' or name = 'Jon'
it Deletes all records of Jon. I want to delete just one record of Jon.
One obvious solution is to use AND rather than OR:
DELETE FROM AddClient
WHERE cId = 'CL-J' AND name = 'Jon';
This will still result in deleting two records, so it is not exactly what you want. But the logic seems clearer (to an outsider).
If your SQLite has the functionality enabled, then you can use:
DELETE FROM AddClient
WHERE cId = 'CL-J' AND name = 'Jon'
LIMIT 1;
(Read here about the optional LIMIT clause for DELETE.)
Another option is to use rowid:
DELETE FROM AddClient
WHERE rowid = (SELECT MIN(rowid)
FROM AddClient
WHERE cId = 'CL-J' AND name = 'Jon'
);
Well, when you say "I want to delete just one record of Jon." You need to specify which other attribute (column) of the tuple (row) you wish to delete. The problem with using an AND operator is that ALL of the rows that have the name attribute of 'John' also have the cId attribute of 'CL-J' so, in effect, you are still deleting the same amount of rows as you would if you used an OR operator.
I also observe that the Id columns are identical as well for ALL records with name 'John', the only differentiating column is amount. Therefore, if you truly wish to delete only one record of John, you're ONLY option is the following:
DELETE
FROM AddClient
WHERE name = 'John'
AND amount = 6000;
I hope this helps you. Cheers!
if you are using Mysql
DELETE FROM AddClient WHERE NAME='john' LIMIT 1;
or you can also delete single record on the basis of amount b/c amount differs of each record.....
DELETE
FROM AddClient
WHERE name = 'John'
AND amount = '5000';
I have a situation where i need to update a row in table and when faced with a duplicate entry then take a decision based on another column value.
For example let's say my table is like this : salary_table
Salary Username Usersurname Last_entry_date
3000 abc bak 20-feb-13
4000 sdf kup 20-mar-15
5000 abc bak 20-mar-15
so my update query is something like this
update salary_table
set salary=9000
where username=abc
and usersurname=bak;
For records like row 2 when there is unique entry this will not cause any problem
but i will get multiple rows for records like 1 (1 and 3) but i only want to update one row. In this case i would like to check last_entry_date. And the entry which has latest date (row 3 in this case) should get updated.
How can it be done ?
Any help would be highly appreciated.
Update salary_table
set salary = 9000
where username= 'abc'
and usersurname= 'bak'
and Last_entry_date = (select max(Last_entry_date)
from SalaryTable
where s.username = username
and s.usersurname = usersurname);
you have to add "where clause" on what you want in this case
"last_entry_date = ??"
With out adding proper filter how you identify which row to be updated.
Let's say I have a table Geometry and another table Customer like this:
Geometry Customer
City ID Location
---- -- --------
Berlin 1 (null)
Paris 2 (null)
London
Now I'd like to fill the column Location with data from the column City. "Randomly" would be nice but it doesn't matter at all.
I've tried
update Customer set Location = (select City from Geometry where rownum < 3);
but still getting this error: single-row subquery returns more than one row
UPDATE: I'd like to fill the whole column Location with one update statement. I'm using ORACLE. The result should look like this:
Customer
ID Location
-- -------
1 Berlin
2 London
Does someone have any better idea?
Thank you very much!
SQL Server:
UPDATE
Customer
SET
Location = (SELECT TOP 1 City FROM Geometry ORDER BY NEWID());
Since you just want it to pick one record at "random", you need to specify the correct number of rows:
update Customer set Location = (select City from Geometry where rownum = 1);
But note that since the subquery is not correlated to the Customer at all, the subquery may be optimised to only run once, and the same (randomly chosen) City will probably be used to update all Locations.
I would do the following, create a trigger on customer:
CREATE OR REPLACE TRIGGER customer_location
BEFORE INSERT OR UPDATE OF location ON customer
FOR EACH ROW
WHEN (NVL(new.location, 'X') = 'X')
BEGIN
SELECT city INTO :new.location
FROM (
SELECT city FROM geometry
ORDER BY DBMS_RANDOM.VALUE
) WHERE rownum = 1;
END;
/
UPDATE customer SET location = 'X';
This will update the customers table with a matching, "random" location. The trigger will also produce a new "random" location when a record is INSERTed into customers - as long as the location to be inserted is 'X' or NULL. (This wouldn't be wise if you actually have a location X - plug in some other value there!)
It is very possible to write a stored procedure to do the same thing, but you would have to create a cursor to loop over all the rows of customers where location is NULL.
update customer set location =
(select city from (
select distinct c.id, g.city, dbms_random.value from customer c, geometry g
order by value, city, id
) randoms where randoms.id = customer.id and rownum=1);
distinct necessary if there were two equal random values for one id
How can I change all the values of a single column for other ones in one single order?
For example, I want to change old values of the last column salary (2250,1,3500,1) for new ones (2352,7512,4253,1142). I have this database:
I know how to do it but changing step by step, and it is not efficient if I have a lot of rows. This way:
UPDATE TABLE tablename
SET salary = REPLACE(tablename.salary, 2250, 2352);
and then perform that operation multiple times.
UPDATE TABLE tablename
SET salary = 2250
WHERE salary = 2352
I'm not sure what you're aiming for with the REPLACE() function but if you want to change the values then you need to do it like the above code. Set the salary to what you want WHERE it has a salary of 2250.
You can write it a few times with the different criteria and then run it.
EDIT: Since you're worried about doing this numerous times you can create a table called salaries:
CREATE TABLE t_salary AS
SELECT salary from tablename;
ALTER t_salary add newsalary integer after salary;
In the 'newsalary' column you can add what the new salary should be then do an inner join. I just created a table for this purpose called stackoverflow (which would be your 'tablename'
update stackoverflow s
inner join t_salary ns on s.salary = ns.salary
set s.salary = ns.newsalary;
Now what this will do is join tablename to t_salary where the current salary = the salary in t_salary. Then you set the tablename.salary equal to the new salary, this worked for me I hope it works for you.
Note, the syntax may be slightly different since I don't have Oracle installed on my home machine, I used MySQL.
Since you already a list old salary values and their corresponding new salary values you can place them in a flat file and create an external table in oracle to point to this file.
Once that is done then you can just fire a simple update statement similar to the one given below:
update test1 set salary = ( select newsalary from test2 where test1.empid = test2.empid);