How to inner join 3 tables in SQL with different path - sql

I want to do an inner join adding the database table SPF but different paths and table CSF, also different paths. For the RSG table using the path of the string connection.
Please recommend.
Table CSF
Path : C:\STACK\ADMIN
NMA
RFN
TATI
B.01.01.001.033
Table SPF
Path: C:\STACK\ADMIN
NMA
RFN
TATI
B.01.01.001.033
Thanks
SELECT s.NMA
FROM CSF AS s
INNER JOIN RSG AS c ON c.SAC = s.RFN
WHERE PNM = #PNM

Related

Join four tables together when only two tables are connected to the junction table

I have four tables created and I need join all the four tables and get the result. Following is the table structure. The CAT table is the junction table here and from it, it has one to many relations to CATFOOD and CUBS tables. From the CATFOOD table, there's another one to many relationship to the table BRAND.
I tried the following query to first join the BRAND and CATFOOD tables and next join the result of that with CAT table. But it didn't work. Please note that I also join the table CUBS with the table CAT too. Here's what I tried,
SELECT CAT.CAT_ID,
CAT_TYPE,
CAT_COLOR,CUBS.CUB_ID,
CUBS.CUB_NAME,
CATFOOD.CATFOOD_TYPE,
CATFOOD.CATFOOD_ID,
CATFOOD.CATFOOD_STATUS,
CATFOOD.SELLER_ID,
BRAND.BRAND_ID,
BRAND.FLAVOUR
FROM CAT RIGHT JOIN CUBS
ON CAT.CAT_ID = CUBS.CAT_ID
RIGHT JOIN (
SELECT CATFOOD.CATFOOD_ID,
CAT_ID,
CATFOOD_TYPE,
SELLER_ID,
CATFOOD_STATUS,
BRAND.BRAND_ID,
FLAVOUR
FROM CATFOOD RIGHT JOIN BRAND
ON CATFOOD.CATFOOD_ID = BRAND.CATFOOD_ID)
AS TEMP ON CAT.CAT_ID = TEMP.CAT_ID
WHERE CAT.CAT_ID = 'some_id_in_the_db';
When I execute this, I get the following error.
Unknown column 'CATFOOD.CATFOOD_TYPE' in 'field list'
When I remove the columns that the error message mention from the select statement, it doesn't give me any errors but returns an empty result.
What I simply want to achieve is get all the details related to a provided CAT_ID so in the java level, I can construct the response appropriately.
Please if anyone know how to achieve this, I appreciate it very much. Thanks in advance.
Update: previously, CATFOOD_TYPE field was mistakenly mentioned as CARFOOD_TYPE in the question. It is corrected now.
You are joining a subquery which contains the column CATFOOD.CATFOOD_TYPE, but this column is not visible to the outer query by that alias/name.
You should alias the subquery, say t and refer to the column as t.CATFOOD_TYPE.
But there is no need to complicate the requirement.
You can join the table cat with all the other tables with LEFT joins:
SELECT c.cat_id, c.cat_type, c.cat_color,
cu. cub_name,
f.catfood_type, f.catfood_id, f.catfood_status,
b.brand_id, b.flavour
FROM cat c
LEFT JOIN cubs cu ON cu.cat_id = c.cat_id
LEFT JOIN catfood f ON f.cat_id = c.cat_id
LEFT JOIN brand b ON b.catfood_id = f.catfood_id
WHERE c.cat_id = 'some_id_in_the_db';
You got a typo, should be CATFOOD_TYPE, not CARFOOD_TYPE
I think you can try this =>
SELECT DISTINCT c.CAT_ID,
CAT_TYPE,
CAT_COLOR,cb.CUB_ID,
cb.CUB_NAME,
cf.CATFOOD_TYPE,
cf.CATFOOD_ID,
cf.CATFOOD_STATUS,
cf.SELLER_ID,
bd.BRAND_ID,bd.FLAVOUR
FROM CAT c
LEFT JOIN CUBS cb ON c.CAT_ID=cb.CAT_ID
LEFT JOIN CATFOOD cf ON c.CAT_ID=cf.CAT_ID
LEFT JOIN BRAND bd ON cf.CATFOOD_ID=bd.CATFOOD_ID

How to join 3 tables without duplicate columns?

Query :
SELECT *
FROM dbo.employer_job
LEFT JOIN dbo.employer_user
ON dbo.employer_job.employer_id = dbo.employer_user.employer_user_id
LEFT JOIN dbo.company_profile
ON dbo.company_profile.company_id = dbo.employer_user.company_id
Duplicate column results :
dbo.employer_job schema :
dbo.employer_user schema :
dbo.comnpany_profile schema :
How do I remove the duplicate company_id column? My Python app won't accept duplicated columns from the database. Most suggest to use left join but that's not solving the issue.
Don't use *, but list the columns you want from each table (preferably with an alias).
SELECT EJ.job_id, EJ.employer_id, ....
FROM dbo.employer_job EJ
...
It's verbose, but how else would the database engine know what you'd like to see?
You need to list the columns explicitly -- and qualify them:
SELECT ej.*, eu.employee_user_email,
cp.company_name, . . .
FROM dbo.employer_job ej LEFT JOIN
dbo.employer_user eu
ON ej.employer_id = eu.employer_user_id LEFT JOIN
dbo.company_profile cp
ON cp.company_id = eu.company_id;
I'm not sure if the LEFT JOIN is really needed. Note that this introduces table aliases, so the query is easier to write and to read.

SQL SELECT Statement Using Three Tables

I am trying to write a SQL Select statement to port some data from a Sybase environment to a MongoDB environment and I'm just trying to figure out the correct syntax to involve three different tables.
Basically what I need to do is do an INNER JOIN on two tables, and then do a matching check against a 3rd table. The three table names are "ar_notes", "customer_service_xref" and "service_notes_details"
This is what I tried:
SELECT * FROM ar_notes arn
LEFT JOIN customer_service_xref csx ON arn.customer_service_xref_id = csx.id_number
WHERE arn.visit_date = service_notes_details.date_of_visit
This doesn't work. I get a correlation error.
What should the syntax look like when involving three tables like this?
You said you need an INNER JOIN among three tables but your query does a LEFT JOIN between two and tries another join in the WHERE clause without refering that table in the FROM clause.
To just fix your query:
SELECT *
FROM service_notes_details snd, ar_notes arn
INNER JOIN customer_service_xref csx ON arn.customer_service_xref_id = csx.id_number
WHERE arn.visit_date = snd.date_of_visit
This is what you should use in current SQL syntax:
SELECT *
FROM ar_notes arn
INNER JOIN customer_service_xref csx ON arn.customer_service_xref_id = csx.id_number
INNER JOIN service_notes_details ON arn.visit_date = service_notes_details.date_of_visit
To be clear, this will only return lines in ar_notes that have corresponding values in customer_service_xref (joining by customer_service_xref_id) and in service_notes_details(joining by visit_date). Your original query, using LEFT JOIN would return lines from ar_notes even if there was no matching customer_service_xref_id.

Joining Two Queries onto a Shared Table

I have been struggling with the concept of combining results of two queries together via a join on a common table and I was hoping I could gain some assistance. The following is a rough guide to the tables:
dbo.Asset (not returned in the SELECT statement, used for joins only)
- dbo.Asset.AssetID
- dbo.Asset.CatalogueID
dbo.WorkOrder
- WorkOrderID
- AssetID
- WorkOrderNumber
dbo.WorkOrderSpare
- WorkOrderID
- WorkOrderSpareID
- WorkOrderSpareDescription
- ActualQuantity
- CreatedDateTime
dbo.Catalogue
- CatalogueID (PK)
- CatalogueNumber
- CatalogueDescription
- CatalogueGroupID
dbo.CatalogueGroup
- CatalogueGroupID
- CatalogueGroupNumber
First Query:
SELECT CatalogueGroup.CatalogueGroupName,
Catalogue.CatalogueNumber,
Catalogue.CatalogueDescription,
Catalogue.CatalogueID,
Asset.IsActive
FROM CatalogueGroup
INNER JOIN Catalogue
ON CatalogueGroup.CatalogueGroupID = Catalogue.CatalogueGroupID
INNER JOIN Asset
ON Catalogue.CatalogueID = Asset.CatalogueID
Second Query:
SELECT WorkOrder.WorkOrderNumber,
WorkOrderSpare.WorkOrderSpareDescription,
WorkOrderSpare.ActualQuantity,
WorkOrderSpare.CreatedDateTime
FROM WorkOrder
INNER JOIN WorkOrderSpare
ON WorkOrder.WorkOrderID = WorkOrderSpare.WorkOrderID
Now I can do the above easily enough in Access (WYSIWYG) by joining Asset/Catalogue/CatalogueGroup together and then joining Asset.AssetID onto WorkOrder.AssetID. I can't seem to get anything similar to work via raw code, I think I have my logic correct for the joins (INNER JOIN on the results of both) but then I am new to this.
Any assistance would be greatly appreciated, any pointers on where I can read further into problems like this would be great.
EDIT: This is what I was trying to use to no avail, I should also mention I am trying to do this in ms-sql, not Access (trying to move away from drag and drop):
SELECT CatalogueGroup.CatalogueGroupName,
Catalogue.CatalogueNumber,
Catalogue.CatalogueDescription,
Catalogue.CatalogueID,
Asset.IsActive,
WorkOrderSpare.WorkOrderSpareDescription,
WorkOrderSpare.ActualQuantity,
WorkOrderSpare.CreatedDateTime,
WorkOrder.WorkOrderNumber
FROM (((CatalogueGroup
INNER JOIN Catalogue
ON CatalogueGroup.CatalogueGroupID = Catalogue.CatalogueGroupID)
INNER JOIN Asset ON Catalogue.CatalogueID = Asset.CatalogueID)
INNER JOIN WorkOrderSpare
ON WorkOrder.WorkOrderID = WorkOrderSpare.WorkOrderID)
INNER JOIN WorkOrder ON Asset.AssetID = WorkOrder.AssetID
Think I see the issue. Assuming that the joins themselves are correct (ie your columns do relate to each other), the order of your joins is a little off - when you join WorkOrder to WorkOrderSpare, neither of those two tables relate back to any table you've identified up until that point in the query. Think of it as joining two tables separately from the chain of joins you have going so far - it's almost like doing two separate join queries. If you switch the last two it should work, that way WorkOrder will join to Asset (which you've already defined) and then you can join WorkOrderSpare to WorkOrder. I've also taken the liberty of removing parentheses on the joins, that's an Access thing.
SELECT CatalogueGroup.CatalogueGroupName,
Catalogue.CatalogueNumber,
Catalogue.CatalogueDescription,
Catalogue.CatalogueID,
Asset.IsActive,
WorkOrderSpare.WorkOrderSpareDescription,
WorkOrderSpare.ActualQuantity,
WorkOrderSpare.CreatedDateTime,
WorkOrder.WorkOrderNumber
FROM CatalogueGroup
INNER JOIN Catalogue ON CatalogueGroup.CatalogueGroupID = Catalogue.CatalogueGroupID
INNER JOIN Asset ON Catalogue.CatalogueID = Asset.CatalogueID
INNER JOIN WorkOrder ON Asset.AssetID = WorkOrder.AssetID
INNER JOIN WorkOrderSpare ON WorkOrder.WorkOrderID = WorkOrderSpare.WorkOrderID
I think you were close. As a slightly different approach to joining, try this:
SELECT CatalogueGroup.CatalogueGroupName,
Catalogue.CatalogueNumber,
Catalogue.CatalogueDescription,
Catalogue.CatalogueID,
Asset.IsActive,
WorkOrderSpare.WorkOrderSpareDescription,
WorkOrderSpare.ActualQuantity,
WorkOrderSpare.CreatedDateTime,
WorkOrder.WorkOrderNumber
FROM
CatalogueGroup, Catalogue, Asset, WorkOrder, WorkOrderSpare
WHERE CatalogueGroup.CatalogueGroupID = Catalogue.CatalogueGroupID
and Catalogue.CatalogueID = Asset.CatalogueID
and Asset.AssetID = WorkOrder.AssetID
and WorkOrder.WorkOrderID = WorkOrderSpare.WorkOrderID
It looks like it should work, but not having data, hard to know if simple joins all the way through is what you want. (It's a matter of personal preference whether to use the and clauses rather than the inner join syntax. While on style preferences, I like table aliases if supported, so FROM CatalogueGroup cg for example, so that you can refer to cg.CatalogueGroupID etc, rather than writing out the full table name every time.)
Ok. so the error that you noted in a comment
The multi-part identifier "WorkOrder.WorkOrderID" could not be bound
is usually when you have an alias for a table and instead of using alias in JOIN you use the table name or when you are using the wrong column name or table name.
Ideally in SQL server your query should look like this
SELECT
cg.CatalogueGroupName,
c.CatalogueNumber,
c.CatalogueDescription,
c.CatalogueID,
A.IsActive,
Wo.WorkOrderNumber,
WoS.WorkOrderSpareDescription,
WoS.ActualQuantity,
WoS.CreatedDateTime
FROM CatalogueGroup cg
INNER JOIN Catalogue c ON cg.CatalogueGroupID = c.CatalogueGroupID
INNER JOIN Asset A ON c.CatalogueID = A.CatalogueID
INNER JOIN WorkOrder Wo ON Wo.AssetID= A.AssetID
INNER JOIN WorkOrderSpare WoS ON Wo.WorkOrderID = WoS.WorkOrderID

SQLITE query, return records from multiple tables each joining their own metatable

I am developing a new database backend for my application for more flexibility.
Instead of one large table with many columns, I now have four tables;
Table "roms" (contains a single column 'Name')
Table "romsmeta" (contains columns 'Name','Title','Year')
Table "software" (contains columns 'System' and 'Name')
Table "softwaremeta" (contains columns 'System','Name','Title','Year')
So now I need to use a JOIN type in my queries, but my skills are a bit rusty. Basically, I would like to perform the following pseudo-query;
SELECT Title,Year from (roms INNER JOIN romsmeta,software INNER join softwaremeta) WHERE Title like '%enteredTitle%'
Obviously, this syntax is not valid.
What query would return the results I'm looking for?
Thanks in advance!
Try this:
SELECT rm.Title, rm.Year
from roms r
inner join romsmeta rm
on r.name = rm.name
inner join software s
on s.name = rm.name
inner join softwaremeta sm
on sm.name = rm.name
WHERE rm.Title like '%enteredTitle%'