Field name from ID on table 1 but name on other table - sql

This is a Firebird database.
First Table
Contacts Company_ID - job_title
Second Table
Client_id - Co_name
In contacts, I want to the job_title field to contain the co_name.
client_id and company_id are the same. Co_name correspond to company_id as well as client_id.
this:
UPDATE Contacts
SET Contacts.Job_title = Clients.co_name
where company_id in (
select client_id from clients
JOIN Contacts c ON Client_id=company_id where record_status='A')
gives me an error as cannot find (clients.co_name)
this other option:
UPDATE Contacts
JOIN Clients ON Clients.Client_id = Contacts.Client_id
SET Contacts.Job_title = Clients.Client_name
gives me an error on JOIN
Any other ideas please?

UPDATE Contacts
JOIN Clients ON Clients.Client_id = Contacts.Client_id
SET Contacts.Job_title = Clients.Client_name

To update a table from another source, you can use MERGE, which only works with Firebird 2.1 or higher:
merge into Contacts
using Clients
on Contacts.Company_ID = Clients.Client_id
when matched then update set Contacts.Job_title = Clients.co_name
Using UPDATE would be possible, but it would get ugly fast because of the lack of support for joined updates, the equivalent query would be something like the code below. I'm not sure if this will work in Firebird 1.5.
update Contacts
set Job_title = (select Clients.co_name from Client where Clients.Client_id = Contacts.Company_ID)
where exists (select * from Client where Clients.Client_id = Contacts.Company_ID)
This might be a bit inefficient because of the two sub-selects that are evaluated independently.

Related

Scalar subquery contains more than one row

Im working with H2 database and wanted to move some data. For that I created the following Query:
UPDATE CUSTOMER
SET EMAIL = SELECT service.EMAIL
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID = customer.CUSTOMER_SERVICE_ID;
When I now perform it in the H2 console I get the following error:
Scalar subquery contains more than one row; SQL statement:
UPDATE CUSTOMER
SET EMAIL = SELECT service.EMAIL
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID = customer.CUSTOMER_SERVICE_ID [90053-192] 90053/90053 (Hilfe)
What is this error telling me?
EDIT
What I want to achiev with my query:
Actually every CUSTOMER has a CUSTOMER_SERVICE. And I simply want to move the COLUMN EMAIL from CUSTOMER_SERVICE to the CUSTOMER Table. for that I already added a email column to the user. I hoped to be able to do it with my query but obviously not.
Your query is not syntactically valid (all subqueries must have parentheses around them).
What you are missing is a correlation clause. I believe you want:
UPDATE CUSTOMER c
SET EMAIL = (SELECT cs.EMAIL
FROM CUSTOMER_SERVICE s
WHERE s.ID = c.CUSTOMER_SERVICE_ID
);
I don't know what this is supposed to be: [90053-192] 90053/90053 (Hilfe).
Your select query is returning more than one row. If you don't want it to, then you need to do something like an aggregate or LIMIT 1 or something similar.
Your sub-query for at least one of your customers has multiple email addresses.
You could ... (Select top 1 serverice.email ...
Or ... (Select max(serverice.email) ...
Update Customer Set EMail=B.Email
From Customer A
Join (Select ID,max(EMail) as EMail From CUSTOMER_SERVICE Group By ID) B
on (A.CUSTOMER_SERVICE_ID = B.ID)
I've spend a lot time with this error type (there shoudn't be duplicates in DB).
At last I've found problem via SQL with COUNT like this:
UPDATE CUSTOMER
SET EMAIL = SELECT COUNT(service.EMAIL)
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID =
customer.CUSTOMER_SERVICE_ID;
And then select problem rows with EMAIL!='1'

SQL: Showing data from two tables together

I'm just starting in the SQL world, so I have a very noob question:
I have 2 tables:
clients (columns: client_id and name)
accounts (columns: account_id and client_id)
and I need to write a query that shows the accounts of all the clients.
But, the problem is that not all the clients have accounts, if the client doesn't have one: how can I show the client_id, the name and NULL for the account_id column?
This query should work:
SELECT *
FROM accounts
LEFT [OUTER] JOIN clients
ON accounts.client_id = clients.client_id;
if not try this one:
SELECT *
FROM accounts
LEFT [OUTER] JOIN clients
ON accounts.client_id = clients.client_id WHERE clients.client_id IS NOT NULL;
These are plain SQL queries, I mean they are not PL-SQL specific. LEFT [OUTER] JOIN will only returns the columns of accounts table. [OUTER] keyword is optional, it defers from database version to version. ON accounts.client_id = clients.client_id will match client_id columns in both tables. Lastly, WHERE clients.client_id IS NOT NULL part should prevent the rows with NULL values in client_id cells.
Useful link: https://www.techonthenet.com/oracle/joins.php
Try this query it returns the clients name client id and shows null to those client who has no accountid.
select clients.name, accounts.account_id from accounts left join clients on
accounts.clintid=clients.client_id

SQL Server View get count from related table where column = "True"

I have two related tables in SQL Server in a 1 to many relationship (Applicant, Reference).
I want a view that will retrieve all the data from the Applicant table and also add another column to the view that tells me how many related Reference rows there are where the "Complete" column is True.
Something like this using a subquery:
select applicantfield1, applicantfield2,
(select count(*) from
reference where reference.applicantkey = applicant.applicantkey
and reference.complete = 1) AS referencecount
from applicant
Unless the complete field is in the applicant table (not the reference table). If so, it would be more like this:
select applicantfield1, applicantfield2,
(select count(*) from
reference where
reference.applicantkey = applicant.applicantkey) AS referencecount
from applicant
where applicant.complete = 1
Something like the below. You need to join on a table. This will handle where the applicant also doesn't have a True reference.
SELECT A.*, isnull(r.comptotal,0) as CompleteTotal
FROM Applicant as a Left Join
(SELECT ApplicantId, Count(Complete) as comptotal
FROM Reference Where Complete=1 Group by ApplicantID) as r
on a.ApplicantId = r.applicantId

SQL Update one table comparing info from two tables

I have the following problem:
Let's suppose I defined TWO tables
USERS
ID (int. key)
NAME (String)
SALARY (currency)
USERSADD
ID (int. key)
TYPE (String)
The 2nd table stores additional information for USERS. Obviously the real tables are more complicated but this is the idea. (Don't ask me why another table is created instead of adding fields to the first table, this is my boss's idea).
Now I am trying to UPDATE the first table if a condition from second table is satisfied.
Something like this:
UPDATE USERS U, USERSADD A
SET U.SALARY = 1000
WHERE U.ID = A.ID
AND A.TYPE = 'Manager'
In Netbeans Derby I have an error: ", found in column X", and it refers to the comma between the two tables (UPDATE USERS U, USERSADD A). I hope I was clear enough...
Would somebody be kind enough to provide me with a solution? Thanks in advance.
UPDATE USERS
SET SALARY = 1000
WHERE ID IN (
SELECT ID FROM USERSADD
WHERE TYPE = 'Manager')
UPDATE USERS
SET USERS.SALARY = 1000
FROM USERS JOIN USERSADD ON USERS.ID = USERSADD.ID
WHERE USERSADD.TYPE ='MANAGER'
The syntax you are using uses an implicit INNER JOIN. It would be better for you to use an explicit join. Try something like this:
UPDATE Users
SET Salary = 1000
FROM Users u
INNER JOIN Usersadd a on u.id=a.id
AND a.Type = 'Manager
UPDATE USERSU
SET SALARY = 1000
WHERE exist IN (
SELECT ID
FROM USERSADD A
WHERE TYPE = 'Manager'
AND U.id = A.id
)

How can I update a field in one table with a field from another table? (SQL)

Two tables:
COURSE_ROSTER - contains
COURSE_ID as foreign key to COURSES
USER_ID as field I need to insert into COURSES
COURSES - contains
COURSE_ID as primary key
INSTRUCTOR_ID as field that needs to be updated with USER_ID field from COURSE_ROSTER
What would the UPDATE sql syntax be? I am trying this, but no good... I'm missing something and I can't find it online.
UPDATE COURSES
SET COURSES.INSTRUCTOR_ID = COURSE_ROSTER.USER_ID
WHERE COURSE_ROSTER.COURSE_ID = COURSES.COURSE_ID
Update Courses
SET Courses.Instructor_ID = Course_Roster.User_ID
from Courses Inner Join Course_Roster
On Course_Roster.CourseID = Courses.Course_ID
This is assuming that your DBMS allows for joins on your update queries. SQL Server definitely allows this. If you cannot do something like this you need to look towards using a subquery.
Not all database vendors (SQL Server, Oracle, etc.) Implement Update syntax in the same way... You can use a join in SQL Server, but Oracle will not like that. I believe just about all will accept a correclated subquery however
Update Courses C
SET Instructor_ID =
(Select User_ID from Course_Roster
Where CourseID = C.Course_ID)
NOTE: The column User_ID in Course_Roster would probably be better named as InstructorId (or Instructor_Id) to avoid confusion
UPDATE COURSES
SET COURSES.INSTRUCTOR_ID = COURSE_ROSTER.USER_ID
FROM COURSES
INNER JOIN COURSE_ROSTER
ON COURSE_ROSTER.COURSE_ID = COURSES.COURSE_ID
Why do you need the column course.instructor_id if you fill it with COURSE_ROSTER.user_id? Isn't it redundant storage?
UPDATE COURSES
SET INSTRUCTOR_ID = CR.USER_ID
FROM COURSES C
INNER JOIN COURSE_ROSTER CR
ON CR.COURSE_ID = C.COURSE_ID