Postgres Update multiple rows - sql

I need to update multiple rows in a table.
i have table customer and contact
I need to update customer where the linked contact in the contact table has the column city at a certain value.
I can get the rows i need with this query
Select cus.id, con.city
from customer cus, contact con
where cus.contacts_id=con.id
and con.city="MyValue"
I know how to update one table but do not understand how to update the table when the rows are looked up from a different table.

Firstly, please do not use the old JOINs (FROM comma separated tables).
Secondly, here you go:
UPDATE customer SET whatever = 'whatever value'
WHERE contacts_id IN (
SELECT id FROM contact WHERE city="MyValue"
)

Related

Inserting values from another table into a column where values are null

I have a table A with ID's only and another table B with two columns, ID and Product Number. The ID column in table B has nulls and Product Number has Product Numbers. I would like to update table B with the ID's in column in no specific order just so that the Product Number has ID's.
I have tried to use update but that has not worked, have tried insert but it just adds the ID's in A to the bottom of the list in B. Would like to do this in Microsoft SQL.
SQL code tried:
IF OBJECT_ID('tempdb..#ProductNum') IS NOT NULL DROP TABLE #ProductNum
SELECT ID
INTO #ProductNum
FROM Products
UPDATE [ProductCatalogue] PC
SET
PC.ID = Pn.ID
FROM #ProductNum Pn
INNER JOIN
[ProductCatalogue] PC
ON Pc.ID = Pn.ID
WHERE Pc.ID IS NULL
It sounds a lot like you would be better off having the ID-Column Autoincrement, instead of giving it the IDs from table A. This is already explained in this answer.
In case you actually need the specific IDs from table A, this SO thread might help you.
Solved the issue by creating auto increment columns on each table and called it Row_ID. Then I used Row_ID to join the tables together with some logic provided by #Chris above.

SQL Computed Column, CountIF 2 Tables

I have two tables
tblData_VendorMasterSSPaymentTerms
tblData_VendorMasterSSPaymentTermsCLM
tblData_VendorMasterSSPaymentTerms contains a field labled VMSSPayTerms_AribaContractID which the values exist in table tblData_VendorMasterSSPaymentTermsCLM
So in table tblData_VendorMasterSSPaymentTermsCLM I want to create a calculated column that counts how many records in tblData_VendorMasterSSPaymentTerms contains the Contract ID for that record.
This is what I have put together so far but it is still coming up with an error
SELECT Count(VMSSPayTerms_AribaContractID)
From tblData_VendorMasterSSPaymentTerms
Where VMSSPayTerms_AribaContractID=VMSSPayTermsCLM_ContractID
Can someone help me identify what I am doing wrong here?
You must join the tables, group by VMSSPayTermsCLM_ContractID and count:
select
c.VMSSPayTermsCLM_ContractID,
count(t.VMSSPayTerms_AribaContractID) counter
from tblData_VendorMasterSSPaymentTermsCLM c inner join tblData_VendorMasterSSPaymentTerms t
on t.VMSSPayTerms_AribaContractID = c.VMSSPayTermsCLM_ContractID
group by c.VMSSPayTermsCLM_ContractID

Select all rows from table A join table B on account number, but exclude rows where tableB.foo == bar. Can I get this data set in one query?

Table A contains basic customer information including account number. Each valid account number appears in exactly one row in table A.
Table B contains information about which flags are set for which customers.
Columns in Table B are account number, flag key, flag value. Most account numbers appear in multiple rows in Table B, one for each flag that is set on that account. However some account numbers do not have any flags set and do not appear at all in table B. Flags do not have default settings and can be undefined. If there is no row in table B for a particular account number and flag key combination, that flag is undefined on that account.
The only flag we care about for this question is flag key Foo. There are many customers who have Foo set to something and many who do not have it set at all.
Goal: Select all the customer info from table A for all customers that do not have flag Foo set at all.
My current solution that works is doing 2 queries and then processing the results. One query selects all rows from table A. The other query selects all rows from table A join table B on account number where tableB.FlagKey=Foo.
Then some code I wrote just iterates through the results of the table B query and generates a list of account numbers. Then it iterates through the results of the table A query and just keeps rows with account numbers that don't appear in the list of account numbers I built.
This works fine but I feel like this "should" be possible to do in one DB query, but I can't figure out how to write that query. I'm not very experienced in writing DB queries.
Try this-
SELECT *
FROM TABLE_A
INNER JOIN TABLE_B
ON A.ACC_NO = B.ACC_NO
WHERE B.FOO <> 'bar'
Use not exists:
select a.*
from a
where not exists (select 1 from b where b.flagkey = 'foo');
This assumes that "not set at all" means that the key is not in the second table.

Update Table Column From Another Table?

So I have two tables. Say one table has a list of students, a student ID for each student, and a home address for each student.
Then you have another table that has a subset of the students in the first able, (and they are in a completely different order) with updated addresses and a student ID.
I need a query that can match the student ID of the two tables, and thereby update the address from the first table, using what is in the second table.
This is the query I tried, but no luck:
UPDATE Roster, UpdatedRoster
SET Roster.Address = (SELECT Address FROM UpdatedRoster WHERE Roster.StudentID = UpdatedRoster.StudentiD)
WHERE Roster.StudentID = UpdatedRoster.StudentiD
Any help here would be greatly appreciated.
Update: This is on Microsoft Access FWIW.
UPDATE Roster
SET Roster.Address = UpdatedRoster.Address
FROM Roster, UpdatedRoster
WHERE Roster.StudentID = UpdatedRoster.StudentiD

Insert by select statement -oracle

I was just trying to copy the values of one column into an empty column of another table .
The two tables are students and payments .
Students have two columns :
rno - primary key of number type // i filled it with some entries
amount //this is completely empty
payments also have same no of columns :
rno - foreign key referencing rno of students table
amount //this is FILLED
Now to copy amounts column from payments to students , i tried this command
insert into students(amount) select amount from payments ;
now normally this command works like a charm ,but here it is behaving slightly different . It throws an error that
NULL values cannot be inserted into students.rno
I tried reasoning that maybe its due to different number of entries inserted in two tables , but on eqalizing the no . of entries in both the tables , just the same result .
So the question is that how can i copy in such a situation ?
Not quite clear on your requirements, but this will populate the STUDENTS table with the SUM of matching payments in the PAYMENTS table. Is this what you're after?
UPDATE STUDENTS
SET AMOUNT = (SELECT SUM(PAYMENTS.AMOUNTS)
FROM PAYMENTS
WHERE PAYMENTS.RNO = STUDENTS.RNO);
You don't want to add records to the students table (which is what INSERT does) you want to UPDATE existing records.
I am not very familiar with Oracle syntax, but I adapted the answer to this question Update statement with inner join on Oracle to hopefully meet your needs.
UPDATE students
SET students.amount = (SELECT payments.amount
FROM payments
WHERE students.rno = payments.rno)