Selecting observation in SAS then do - sql

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;

Related

How to move SQL row value to other row

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.

Handling duplicates while updating records in SQL using where clause

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.

Fill one column with data from one column in another table (randomly)

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

Replace values in column with Oracle

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);

How can i get the nth row position from table without using limit?

How can i get the nth row position from table without using limit ?
I have a table with four fields id,name,country,description. The query is returning by condition country = 'asia'. The total number of records is more than hundred. Now what i need is if i hav a name 'test' in 23rd position in table then how can i get the position 23rd without using limit and auto increment id ( auto id doesn't give the exact position where i am using a condition over my query)
name should not contain the duplicate record.
I just want the position to check through another query (for pagination).
Is there any function available in mysql to achieve this with out using limit?
I have got some idea through this but i cant get my expected mysql result through the answers.
Thanks.
Since SQL does not have concept of implicit "row position", you should define the row order by sorting the rows on a column or an expression.
This assumes your row positions are identified by the field id (i. e. rows with higher id come later).
SELECT (
SELECT COUNT(*)
FROM mytable mi
WHERE country = 'asia'
AND mi.id <= mo.id
)
FROM mytable mo
WHERE country = 'asia'
AND name = 'test'