Insert into newly created column - sql

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)

Related

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

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;

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 ;

Select records from table A where 4 of its columns equal to 4 of table's B columns

Basically, I have two tables, a [students] table and a [units_allocation] table.
Both tables have columns named course_abbr, course_name, month_of_admission and year_of_admission.
I want to select records from the students table where the above 4 columns in both tables have similar values.
An inner join or exists is a typical solution:
select s.*
from students s
where exists (select 1
from units_allocation ua
where s.course_abbr = ua.course_abbr and
s.course_name = ua.course_name and
s.month_of_admission = ua.month_of_admission and
s.year_of_admission = ua.year_of_admission
);
One way is using INTERSECT(work's for SQL Server and Oracle)
SELECT course_abbr, course_name, month_of_admission,year_of_admission
FROM students
INTERSECT
SELECT course_abbr, course_name, month_of_admission,year_of_admission
FROM units_allocation
or use EXISTS in case of Mysql
SELECT *
FROM students s
WHERE EXISTS (SELECT 1
FROM units_allocation u
WHERE s.course_abbr = u.course_abbr
AND s.course_name = u.course_name
AND s.month_of_admission = u.month_of_admission
AND s.year_of_admission = u.year_of_admission)

How to merge two tables with having same column name

select *
INTO [dbo].[aTable]
from dbo.bt btg left join dbo.btt bta on btg.specialty = bta.specialty
order by 1, 6
I am getting the following error:
Column names in each table must be unique. Column name 'Specialty' in table 'aTable' is specified more than once.
Columns for bt table:
Location
Specialty
Provider
Column for btt table:
Specialty
Topic
I am trying to get Location, Specialty, Provider, and (join all topics for the specialty).
The issue here is the "Select *", which will select all fields in your result set. Try specifying the specific fields that you want to insert.
For instance:
SELECT Location, btg.Specialty, Provider, Topic
INTO INTO [dbo].[aTable]
from dbo.bt btg left join dbo.btt bta on btg.specialty = bta.specialty
order by 1, 6
Alias name is all you need to mention to avoid conflict when you join two tables with same column names.
SELECT btg.Location, btg.Specialty, btg.Provider,btg.Topic
INTO INTO [dbo].[aTable]
from dbo.bt btg join dbo.btt bta on btg.specialty = bta.specialty
order by 1, 6
you can do something like this:
select btg.*, topic
INTO [dbo].[aTable]
from dbo.bt btg left join dbo.btt bta on btg.specialty = bta.specialty

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;