SQL Copy data in joined tables - sql

I have created a query in SQL to join 3 tables :
1st table contains contact data,
2nd table contains link data,
3rd table contains sales info.
The 2nd table is like an index table to join the sales data (3rd table) to the contact data (1st table).
I have managed to join the 3 tables together with the following which only lists out contacts that have sales info associated:-
USE wce_site
SELECT
c.CONTACT,c.POSTALCODE,c.UNIQUEID,l.LEntityID,l.LETableName,l.LUniqueID,s.Area,s.POSTCODE
FROM
dbo.wce_contact AS c
INNER JOIN dbo.wce_linkto AS l
ON c.UNIQUEID=l.LEntityID
INNER JOIN dbo.wce_sales AS s
ON s.UNIQUEID=l.LUniqueID
Now I have the desired results I don't know what to do next to copy c.POSTALCODE to s.POSTCODE which are in 2 different tables.

In SQL Server, you can use join with update, so I think the following does what you want:
UPDATE s
SET POSTCODE = c.POSTCODE
FROM dbo.wce_contact AS c INNER JOIN
dbo.wce_linkto AS l
ON c.UNIQUEID = l.LEntityID INNER JOIN
dbo.wce_sales AS s
ON s.UNIQUEID = l.LUniqueID;

Related

Need help in joining tables to get the accurate results

I have 4 tables, the 1st table 'LD0P0K' is the main table i need to join with 2nd 'LD0P0K01' and 3rd 'LD0P0K04' with the 1st column value 'HUSHLNR' and get the 'PNR' from both tables and then join with last table 'LD0P0A' to get the values with 'PNR'.
I tried the below solution but its not giving the data from 3rd table and giving 6 records with 2 rows for each 2nd table.
Select HS.HUSHLNR, HS.FOMDAT,HS.TOMDAT,HSM.PNR,HSM.FAMHUVUD,HSM.MARK,HSM.AVFOMDAT,HSM.AVTOMDAT,P.KUNDNUMMER,P.FODDAT from LD0P0K HS
LEFT OUTER JOIN LD0P0K01 HSM on HS.HUSHLNR = HSM.HUSHLNR
LEFT OUTER JOIN LD0P0K04 CHM on HS.HUSHLNR = CHM.HUSHLNR
LEFT OUTER JOIN LD0P0A P on p.PNR= HSM.PNR AND p.PNR = CHM.PNR
Where HS.HUSHLNR='906'
You can simply use Union to combined the fields
Here is the link for Union to understand
SQL Union
and here is the code change it whatever you want
Select HS.HUSHLNR as HUSHLNR, HS.fromdate as FromDte,HSM.PNR as PNR from LD0P0K HS
inner JOIN LD0P0K01 HSM on HS.HUSHLNR = HSM.HUSHLNR
union
Select CHM.HUSHLNR as HUSHLNR, '' as FromDte,CHM.PNR as PNR from LD0P0K04 CHM
order by PNR
Output:

Join tables when 3 column in first table that can point to same column in second table

I have the following DB structure:
And right now I can't make up a query to get
a creator data, admin data and tech data from item_contacts...
What kind of JOIN I need to use and how?
I think you want 3 joins on item_contacts - one for each column whose data you want to recover:
select
i.*,
cc.data as creator_data,
ca.data as admin_data,
ct.data as tech_data
from items i
inner join item_contacts cc on cc.contact_id = i.creator_id
inner join item_contacts ca on ca.contact_id = i.admin_id
inner join item_contacts ct on ct.contact_id = i.tech_id

SQL Server Inner join on UID

I am trying to do an inner join on 2 tables having same names, but in different databases (database A and B). The table is as below:
My aim is to update columns MinMultiplyFactor and MaxMultipleFactor in the table in database A based on 2 conditions (NodeId and Year should match with the other table).
I wrote a query:
update DB_A.[dbo].[TABLE]
set DB_A.[dbo].[TABLE].MinMultiplyFactor = B.MinMultiplyFactor
FROM DB_A.[dbo].[TABLE] A INNER JOIN DB_B.[dbo].[TABLE] B
ON A.NodeId = B.NodeId
and A.Year = B.Year;
But it's saying 0 rows updated even when there are a lot of common Node Ids and years. The NodeId is a uniqueIdentifier in the table. May be that's the issue?

I want to retrieve data from 4 tables in SQL Server

I want to retrieve data from 4 tables. Patient table has id as PK which is the foreign key in other three tables ett, phar and ssc. Where a patient lie in only one category. i.e patient id pt1 exists in either of the 3 tables. now I want to retrieve patient info along with its associated category.
My query is:
SELECT *
FROM Patient p
INNER JOIN ETT t
ON p.Patient_ID = t.Patient_ID || INNER JOIN Pharmacological ph
ON p.Patient_ID = ph.Patient_ID
I used OR clause because I want only 1 inner join executing at one time. but its not giving me results, any suggestions??
....Patient table has ID as PK which is the foreign key in other three
tables name: ett, phar and ssc where a patient lie in only one
category. Example, patient id pt1 exists in either of the 3 tables.
Based on your statement, you can join all the tables in table Patient using LEFT JOIN since a record can only exist on one table. The query below uses COALESCE which returns the first non-null value with int the list.
The only thing you need is to manually specify the column names that you want to be shown on the list as shown below.
SELECT a.*,
COALESCE(t.colA, p.ColA, s.ColA) ColA,
COALESCE(t.colB, p.ColB, s.ColB) ColB,
COALESCE(t.colN, p.ColN, s.ColN) ColN
FROM Patient a
LEFT JOIN ETT t
ON a.Patient_ID = t.Patient_ID
LEFT JOIN Phar p
ON a.Patient_ID = p.Patient_ID
LEFT JOIN SSC s
ON a.Patient_ID = s.Patient_ID
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
For or - do not ise ||, use "or"
You cannot join with or, you need re-format your query.

Oracle/plsq Inner join multiple column to table one column

I have problems joining 2 table let say I have a column on the 1st table name nationalitycode(this is number) and it fetches the description(it could be american, chinese etc) depending on the first table nationalitycode. So I tried inner join the table so the first inner join is good and working perfectly.
here is the code:
SELECT person.firstnm, person.middlenm, person.lastnm, refcd.description
FROM person
INNER JOIN refcd
ON person.natcd = refcd.id;
However when I try to join the 2nd column from the 1st table let say the column name is gencd(gendercode its a foreign key) but when I updated the query to inner join 2 column from one table getting the value from one column in the 2nd table I get this error.
"Column ambigously defined" I get this error and I understand this my question is is there anyway to achieve my desired output? I want to join 2 table, specifically joining 2 column on table 1 to 1 column in table 2.
Here is my updated query:
SELECT person.firstnm, person.middlenm, person.lastnm, refcd.description
FROM person
INNER JOIN refcd
ON person.natcd = refcd.id
INNER JOIN refcd
ON person.gencd = refcd.id;
Help is really appreciated.
Your query should be
SELECT person.firstnm, person.middlenm, person.lastnm, refcd.description
FROM person INNER JOIN refcd ON person.natcd = refcd.id
INNER JOIN refcd R2 ON person.gencd = R2.id;