Update all column rows with random values from another table in Snowflake - sql

I need to update all rows of a column with random values selected from another table. I am trying following query -
UPDATE TEST_CITY
SET "CITY" = (SELECT NAME FROM CITY SAMPLE (1 rows))
The subquery gives me a random city when executed separately, but in above case all rows are updated with same value.
I have also tried to select random records by id like following but this also updates all rows with same value -
UPDATE TEST_CITY
SET "CITY" = (select c.name
from city c
where c.id = (SELECT uniform(1, 50, random()))
)
This query for example updates all rows with different random values-
UPDATE TEST_CITY
SET "name" = to_varchar(ABS(MOD(RANDOM(1), 1000000)))
Can I have something equivalent to this when random values are strings and should come from a separate table ?

I don't know specifically for Snowflake, but other databases sometimes optimize subqueries with a volatile function, resulting in a single value.
One solution that I've seen work is to use a correlated subquery:
UPDATE TEST_CITY
SET "CITY" = (select c.name
from city c
where c.id = (SELECT uniform(1, 50, random())) AND
test_city.city is not null -- any condition should do
);
Although the performance is likely to be worse, perhaps order by will work:
UPDATE TEST_CITY
SET "CITY" = (select c.name
from city c
order by random()
limit 1
);

Following query worked for me. I have used hash on column name to make the update work on all rows of my column -
UPDATE "TEST_CITY" SET "CITY" = C."NAME" FROM CITY C WHERE C."ID" =
ABS(HASH("CITY")%16917) + 1 ;
16197 are the number of rows I have in City table.
Thanks

Below code run for me
UPDATE TEST_CITY a SET a.CITY = b.NAME FROM (
SELECT NAME ,row_number() over (order by random()) AS id from CITY) b;

Related

Insert into newly created column

I have a Students table with 2 col as Rollno and Marks with 12 records
I added another column in students table as Name. How do I fill Names with 12 records from another table in SQL Server?
I tried this:
SELECT * FROM [SampleDB].[dbo].[Student_SQL]
insert into Student_SQL(name)
select name
FROM [School].[dbo].[StudentMaster]
update [Student_SQL]
set name = 'David'
where RollNo = 4`
I think you want the upate/join syntax. Assuming that the two tables relate through column rollno:
update ss
set ss.name = sm.name
from student_sql ss
inner join student_master sm on sm.rollno = ss.rollno
The upside of this approach is that it filters the rows and only update those that match in the master table.
in order to do this, you need to have column in common for both tables
update [Student_SQL] s
set name = (select name from other_table o where o.roll_no = s.roll_no)

how to update single column in one table when two column are match in another table?

Everyone I am beginner in SQL.
I have two tables like first, second.
pid column of first table and pid column of second table are same. I have to get price from the first table and quantity from second table, multiply those values (final) and update that final values in second table at the price column.
I tried but it doesn't work. Can anyone help me? Thanks in advance.
FIRST TABLE
Name Null? Type
------ -------- ------------
PID NOT NULL NUMBER(38)
PNAME NOT NULL VARCHAR2(20)
PPRICE NOT NULL FLOAT(126)
PAVAIL NOT NULL NUMBER(38)
SECOND TABLE
Name Null? Type
------------ -------- ------------
ORDERID NOT NULL NUMBER(38)
PID NUMBER(38)
CUSTOMERNAME NOT NULL VARCHAR2(20)
PHNO NOT NULL NUMBER(38)
QTY NOT NULL NUMBER(38)
PRICE NOT NULL FLOAT(126)
I used this query, but it doesn't work:
update first f, second s
SET s.price = f.pprice * s.qty
where s.pid EXISTS (select second.pid from first, second
where (first.pid=second.pid);
Oracle does not support update join syntax. One workaround is to use an update with a correlated subquery.
UPDATE second s
SET s.price = s.qty * (SELECT f.pprice FROM first f WHERE f.pid = s.pid)
WHERE EXISTS (SELECT 1 FROM first f WHERE f.pid = s.pid)
The exists clause is necessary to make sure that a record in second which does not match to anything in first does not have its price update to NULL. If this can't happen, or you don't care if it does, then you may also remove the WHERE clause.
Update using a join is done with the MERGE statement in Oracle and that's what you need. In your case:
MERGE INTO second a
USING FIRST b
ON (a.pid = b.pid)
WHEN MATCHED
THEN
UPDATE SET a.price = b.price * a.qty;
Check this statement:
update second s
set s.price = s.qty * (select f.pprice from first f where f.pid = s.pid )
where exists (select f.pprice from first f where f.pid = s.pid );
you may try this also..
update s
set s.price =
s.qty * f.pprice
from second s,first f
where s.pid = f.pid
if any mistake..revert me back

Update table column based on value of key in other column POSTGRES

My table name is companies having is_nse,a boolean column and exchanges column type json having value like "[{"exchange":"NSE","ticker":"ABC"},{"exchange":"BSE","ticker":"ABC"}]"
Now i have this query
update companies set is_nse=0 from (SELECT is_nse, obj.value->>'exchange' As exch FROM (SELECT * FROM companies WHERE sector is not null) u JOIN LATERAL json_array_elements(exchanges) obj(value) ON obj.value->>'exchange' = 'BSE')y
But it is updating all the rows in companies table rather than rows of the subquery.Can anybody tell me where am I going wrong?
You need to connect the companies in the update to the from clause. Assuming you have an id, you can do:
update companies
set is_nse = 0
from (select c.*
from companies u join lateral
json_array_elements(exchanges) obj(value)
on obj.value->>'exchange' = 'BSE'
where u.sector is not null
) y
where y.companyid = companies.companyid ;

find duplicate pairs in sql table and update values for 1 of each duplicate pair

I have a table with 1800 entries. 900 entries are duplicates of the other 900 by values in a number of fields, example contactFirstName and contactLastName, however one of each duplicate pair contains data which the other does not, example PhoneNumber. The duplicates which contain the additional data, ie: phoneNumber, have a statusID=10 and the duplicates without the phoneNumber data have statusID=2.
How can I find the duplicate pairs by contactFirstName and ContactLastName, then for each pair (remember-there are 900 pairs) copy the phoneNumber data in the duplicate with statusID=2 into the phoneNumber field in the duplicate with statusID=10 (but only if the destination field value is 'NULL').
I hope my explanation is clear. I Would be very grateful of any help.
Join the table with itself, and update the one with status id 10.
Example (SQL Server syntax):
update
a
set
phoneNumber = b.phoneNumber
from
TheTable a
inner join TheTable b on b.contactFirstName = a.contactFirstName and b.contactLastName = a.contactLastName and b.statusID = 2
where
a.statusID = 10 and a.phoneNumber is null
Something like:
UPDATE table
SET PhoneNumber = (SELECT t2.PhoneNumber
FROM table t2
WHERE t2.StatusID = 2
AND t2.contactFirstName = table.contactFirstName
AND t2.ContactLastName = table.ContactLastName)
WHERE PhoneNumber IS NULL
AND StatusID = 10;

oracle: update a column with not null value

I wanna update a column of a table:
UPDATE product prod
SET prod.prod_supplier_id = (SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno
)
the SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno
mustn't return a null result, if it's null, the update will not be made
How to do that?
You need to filter the rows to be updated in the WHERE clause as well:
UPDATE product prod
SET prod.prod_supplier_id = (SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno
)
WHERE EXISTS (SELECT 42
FROM supplier s2
WHERE s2.prodno = prod.prodno);
It might be faster using a MERGE (assuming prodno is the primary key in product):
merge into product
using
(
select p.prodno,
s.prod_supplier_id
from product p
join supplier s on s.prodno = p.prodno
) t on (t.prodno = prod.prodno)
when matched then update
set prod_supplier_id = t.prod_supplier_id;
Not tested!
first of all create a back up table:
CREATE TABLE productBAK AS SELECT * FROM product;
now you can use update query like this:
UPDATE product prod
SET prod.prod_supplier_id = (SELECT s.prod_supplier_id
FROM supplier s
WHERE s.prodno = prod.prodno and
s.prod_supplier_id is not null
)
WHERE prod.prodno in (SELECT s1.prodno FROM supplier s1 where s1.prod_supplier_id is not null);
The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!
I see prod_supplier_id being a column in both product and supplier.
Can you explain in which table the prod_supplier_id must not be null?
Looking at your query you want something like
update product
set prod_supplier_id = (select prod_supplier_id
from supplier
where prod_supplier_id is not null);
Issues you're facing:
all product records will be updated with the same prod_supplier_id
if the subselect statement returns more than one record -let's say you have 5 suppliers, so 5 prod_supplier_id's, the update statement will fail.
Can you tell us:
do you want all your products to be set to 1 supplier?
if not, do you have a list of which product should have which supplier?
R