Looking up another table for values in multiple columns (SQL) - 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

Related

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

SQLite select query if inner join query doesn't exists

I have two tables, one for all the foods which contains food id, default name and other foods values.
Another table is for the food name translations which contains food id, language id, translation.
What i want to do is join between these tables by the food id to get the translation for food id = 5 for example and language id = 1 which is Spanish for example. and i do it by:
SELECT *
FROM Foods
INNER JOIN FoodNameTranslations USING(Food_ID)
WHERE Food_ID = 5
AND Language_ID = 1
Now what if the the 'FoodNameTranslations' table doesn't have a translation for Food_ID 5 and Language 1?
then i want to simply get the food row with food id = 5 from the 'Foods' table (that contains the default name).
How can i make one query that does this? thanks!
You would do a LEFT JOIN and put the language ID into the join condition.
SELECT
COALESCE(t.TranslatedName, f.DefaultName) FoodName
FROM
Foods f
LEFT JOIN FoodNameTranslations t ON t.Food_ID = f.Food_ID AND t.Language_ID = 1
WHERE
f.Food_ID = 5
You cando do this by changing the JOIN condition:
INNER JOIN gets all records that are common between both tables based on the supplied ON clause.
LEFT JOIN gets all records from the LEFT linked and the related record from the right table ,but if you have selected some columns from the RIGHT table, if there is no related records, these columns will contain NULL.
RIGHT JOIN is like the above but gets all records in the RIGHT table.
FULL JOIN gets all records from both tables and puts NULL in the columns where related records do not exist in the opposite table.
Try using LEFT JOIN instead INNER JOIN, you query will be like this:
SELECT *
FROM Foods
LEFT JOIN FoodNameTranslations USING(Food_ID)
WHERE Food_ID = 5
AND Language_ID = 1

Conditional Table Join In SQL Server

I have a table named 'Task' with fields (Id int, TaskName nvarchar, AssigneeType int, AssigneeId int).
AssigneeType can contain 3 int values pointing to specific tables. (0 = User, 1 = Group, 2 = Location)
User, Group, Location are the tables
AssigneeId contains the Id of record in the table pointed by AssigneeType.
Problem Area
I want to extract all tasks by joining task table with the table pointed by AssigneeType.
If AssigneeType contains 0, I need to join Task table with User table.
If AssigneeType contains 1, I need to join Task table with Group table.
If AssigneeType contains 2, I need to join Task table with Location table.
Basically I need to make conditionally join. I have found this, but I dont know that how can I incorporate for my need. I want to show TaskName and Joined Table Record's Name field.
Any Help?
This will do a left join and give you the first name it finds using COALESCE
SELECT Task.*, COALESCE([User].Name, [Group].Name, Location.Name) AS Name
FROM Task
LEFT JOIN [User]
ON [User].Id = Task.AssigneeId
AND Task.AssigneeType = 0
LEFT JOIN [Group]
ON [Group].Id = Task.AssigneeId
AND Task.AssigneeType = 1
LEFT JOIN Location
ON Location.Id = Task.AssigneeId
AND Task.AssigneeType = 2
You cannot join a table or not. You must join all tables. So you will outer join the three tables getting only one match. Then show that match with COALESCE.
select t.*, coalesce(u.name, g.name, l.name) as name
from task t
left join user u on t.assigneetype = 0 and t.assigneeid = u.id
left join [group] g on t.assigneetype = 1 and t.assigneeid = g.id
left join location l on t.assigneetype = 2 and t.assigneeid = l.id;
EDIT: I've corrected my answer and replaced backticks with brackets. Different dbms use different symbols in order to use reserved words such as 'GROUP' for table names. In SQL Server this should be brackets rather than backticks. However, it is always a bad idea to use reserved words for table names and columns, so you might want to change this, if you can.

SQL Copy data in joined tables

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;

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

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