SQL - Join two fields in the same table to a single field in another table - sql

I'm working on a way to lookup the description for a code that appears in two fields of the same table.
The table/field names are :
Contacts
Name, Group_1 and Group_4
Lookup
Lookup_Id, Lookup_Name
Contact.Group_1 and Contact.Group_4 both refer to values in Lookup.Lookup_Id and need to be resolved to their corresponding name values in Lookup.Lookup_Name.
How can I connect both fields to the Lookup table and have them bring back their respective Lookup_name values ?

Left Join Contacts with Lookup twice. Once with Group_1 and once with Group_2. Left Join instead of just Inner Join, as you may have a contact without two groups.
SELECT C.Name,
G1.Lookup_Name,
G2.Lookup_Name
FROM Contacts C
LEFT JOIN Lookup G1 ON G1.Lookup_Id = C.Group_1
LEFT JOIN Lookup G2 ON G2.Lookup_Id = C.Group_4

Like this:
select *
from Contacts c
left join Lookup l1 on l1.Lookup_Id = c.Group_1
left join Lookup l2 on l2.Lookup_Id = c.Group_4

Related

SQL column added twice during INNER JOIN

I am trying to join two tables from a database; energyImport and sunAlt.
energyImport has three columns: timestamp_id, energy, duration.
sunAlt has two columns: timestamp_id, altitude
I am doing an inner join on these two tables using the SQL:
SELECT *
FROM energyImport
INNER JOIN sunAz ON sunAz.timestamp_id = energyImport.timestamp_id;
The output from this is:
timestamp_id,duration,energy,timestamp_id,altitude
1601769600,1800,81310,1601769600,0.0
1601771400,1800,78915,1601771400,0.0
1601773200,1800,78305,1601773200,0.0
The problem is that the timestamp_id column is repeated. How can I join these columns and only include the first timestamp_id?
Replace the * with
energyImport.timestamp_id,energyImport.duration, energyImport.energy,
sunAz.altitude
Either you specify the columns that you want in the results:
SELECT e.timestamp_id, e.duration, e.energy, s.altitude
FROM energyImport e INNER JOIN sunAz s
ON s.timestamp_id = e.timestamp_id;
Or, use NATURAL instead of INNER join, so that the join is based on the columns(s) with the same names of the 2 tables (if this fits your requirement), because NATURAL join returns only 1 of each pair of these columns:
SELECT *
FROM energyImport NATURAL JOIN sunAz;
See the demo.

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

Looking up another table for values in multiple columns (SQL)

I am currently using PostgreSQL and having a table as such:
There are 3 columns which contains the location ID which is linked to a location table. In the location table there is location_id and location_name. Is it possible to create a query that would allow me to display the location_name instead of the location_ids?
Use multiple left joins:
select bt.transaction_id,
bt.datetime,
l1.location_name as location_1_name,
l2.location_name as location_2_name,
l3.location_name as location_3_name
from base_table bt
left join location l1 on l1.location_id = bt.location_1
left join location l2 on l1.location_id = bt.location_2
left join location l3 on l1.location_id = bt.location_3

How to join multiple tables in a view

how to create view of this query anyone help me please, i want create view of this but its show me error
Msg 4506, Level 16, State 1, Procedure ordersview, Line 3 Column names
in each view or function must be unique. Column name 'ID' in view or
function 'ordersview' is specified more than once.
CREATE VIEW ordersview
AS
Select * from UserClaimData cd
Inner join UserClaimDeductions ud on
ud.CLAIMID = cd.ID
Inner join UserClaimApproval ua on
ua.CLAIMID = cd.ID
inner join ClaimDataBreakdown cb on
cb.CLAIMID = cd.ID
inner join AppExpenseTypes ae on
ae.ID = cb.EXPENSETYPE
inner join AppNOWTypes an on
ae.ID = an.EXPENSETYPEID
inner join AppAreas aa on
aa.ID = cb.AREAID
inner join AppZones az on
cb.ZONEID = az.ID
inner join AppRegions ar on
ar.ID = cb.REGIONID
The answer to the question you've asked is to specifically reference elements from each table; for example:
CREATE VIEW ordersview
AS
Select cd.ID AS ID1, ua.ID as ID2, etc... from UserClaimData cd
Inner join UserClaimDeductions ud on
ud.CLAIMID = cd.ID
Inner join UserClaimApproval ua on
ua.CLAIMID = cd.ID
inner join ClaimDataBreakdown cb on
cb.CLAIMID = cd.ID
inner join AppExpenseTypes ae on
ae.ID = cb.EXPENSETYPE
inner join AppNOWTypes an on
ae.ID = an.EXPENSETYPEID
inner join AppAreas aa on
aa.ID = cb.AREAID
inner join AppZones az on
cb.ZONEID = az.ID
inner join AppRegions ar on
ar.ID = cb.REGIONID
I would, however, suggest that you don't put such a complex join inside a view. Consider the columns you want, and perhaps think about a stored procedure, or a table value function.
What part of the error message do you not understand?
You have select *, which brings together all columns from all tables. Just based on the join conditions, it is clear that most tables have an ID column, so there are multiple columns called ID. CLAIMID also seems quite popular.
In general, using select * is discouraged. However, it should not be used for views. A view should state the columns that it contains:
select cd.Id, . . .
Your view has more than one column with the same name, and this is causing the error.
AppRegions has a column called ID
AppAreas has a column called ID
UserClaimData has a column called ID
Change the Select * to specify the columns of the tables you want to have on the View table, and put a particularry name, or just don't put them.
You need to change the name of some columns in the view. Use AppAreas.ID as aaID
A view is like a virtual table, So you can't have the same name for 2(or more) columns.
So to avoid this, in your select Query instead of * provide the column names, and if there are 2 columns with the same name and you need them both in the view, give them different alias names.
Suppose you have ColumnA in TableA and TableB and you want them both in the view. Create view like this
CREATE VIEW vm_Sample
SELECT
A.COLUMNA COLUMNA_1,
B.COLUMNA COLUMNA_2
FROM TABLEA A INNER JOIN TABLE B
ON A.ID = B.ID

How can I put join in 3 tables?

I am having three tables. Main table which I want to show contain all the data whereas the other two tables contain the values whose reference ID's are available in main table. I want to show all the values from main table along with the values whose references are available in other two tables.
PART_ID and SERIAL_ID values are available in Table B and Table C respectively. How can i show the data?
I am using inner join which shows result but not as per my requirement.
Here is my SQL code:
SELECT
TEMP_RMA_ENQUIRY.TEMP_ID,
PART_NUMBER_TBL.PART_NO,
PART_SERIAL.SERIAL_NUM
FROM TEMP_RMA_ENQUIRY
INNER JOIN PART_NUMBER_TBL
ON TEMP_RMA_ENQUIRY.RMA_PART_NO=PART_NUMBER_TBL.PARTID
INNER JOIN PART_SERIAL
ON TEMP_RMA_ENQUIRY.RMA_SERIAL_NO=PART_SERIAL.SERIAL_ID
Use Left Join Instead of Inner JOIN
Try this
SELECT TEMP_RMA_ENQUIRY.TEMP_ID,
PART_NUMBER_TBL.PART_NO,
PART_SERIAL.SERIAL_NUM
FROM TEMP_RMA_ENQUIRY
LEFT JOIN PART_NUMBER_TBL
ON TEMP_RMA_ENQUIRY.RMA_PART_NO=PART_NUMBER_TBL.PARTID
LEFT JOIN PART_SERIAL
ON TEMP_RMA_ENQUIRY.RMA_SERIAL_NO=PART_SERIAL.SERIAL_ID