All parent table rows left jon child table directly - sql

I have three tables: patron, patron_address, and patron_phone
for every patron/
Patron has 1-3 Patron_address rows
patron_address has 0-4
I want to display all the rows from the patron table and all the phone numbers of phone_type = '4'.
However, when I use the query below, I only get rows that have a phone{type of 4, not all the patron rows.
I tried to get Access 2007 query designer to do this, but something is off-kilter. Patron_address rows have an address_type. Only patron_address rows with address_type 1 have a child phone record.
So how do I get all the patron rows regardless of whether they have a patron_phone of phone_type 1?
SELECT
PATRON.patron_id, PATRON_PHONE.PHONE_TYPE,
PATRON_PHONE.PHONE_NUMBER, PATRON_ADDRESS.ADDRESS_TYPE
FROM
(PATRON
INNER JOIN
PATRON_ADDRESS ON PATRON.PATRON_ID = PATRON_ADDRESS.PATRON_ID)
LEFT JOIN
PATRON_PHONE ON PATRON_ADDRESS.ADDRESS_ID = PATRON_PHONE.ADDRESS_ID
WHERE
(((PATRON_PHONE.PHONE_TYPE) = '4'))
ORDER BY
PATRON.patron_id;
If I add the criterion that the address type must equal 1, I get absolutely nothing back, even though this combination exists in the database. Isn't the behavior I want the point of a left outer join? Thanks.

You have an INNER JOIN from the Patron table to Patron_Addr. This means that there must be matching records in both tables (by PATRON_ID) to return a value.
SELECT PATRON.patron_id, PATRON_PHONE.PHONE_TYPE, PATRON_PHONE.PHONE_NUMBER, PATRON_ADDRESS.ADDRESS_TYPE
FROM PATRON
INNER JOIN PATRON_ADDRESS ON PATRON.PATRON_ID = PATRON_ADDRESS.PATRON_ID
LEFT JOIN PATRON_PHONE ON PATRON_ADDRESS.ADDRESS_ID = PATRON_PHONE.ADDRESS_ID
WHERE PATRON_PHONE.PHONE_TYPE = 4
ORDER BY PATRON.patron_id
You have a LEFT JOIN from address to table so there doesn't need to be a matching phone number.
Since you are filtering for a Phone Type of 4, it will ONLY allow records that do have a phone record where the PHONE_TYPE = 4.
Is your Phone Type field a number or text? SQL Server will try to convert them back and forth but others may not and give an error or just not match - I don't remember how Access might handle this situation.
If you remove the PHONE TYPE criteria, your address criteria should work.
If you want to get all records of parton with address 1 but only phone numbers that are TYPE = 4, change the WHERE PHONE_TYPE=4 to part of the LEFT JOIN:
SELECT PATRON.patron_id, PATRON_PHONE.PHONE_TYPE, PATRON_PHONE.PHONE_NUMBER, PATRON_ADDRESS.ADDRESS_TYPE
FROM PATRON
INNER JOIN PATRON_ADDRESS ON PATRON.PATRON_ID = PATRON_ADDRESS.PATRON_ID
LEFT JOIN PATRON_PHONE ON PATRON_ADDRESS.ADDRESS_ID = PATRON_PHONE.ADDRESS_ID AND PATRON_PHONE.PHONE_TYPE = 4
WHERE PATRON_PHONE.ADDRESS_TYPE = 1
ORDER BY PATRON.patron_id
Access SQL:
SELECT PATRON.patron_id, P_PHONE.PHONE_TYPE, P_PHONE.PHONE_NUMBER, PATRON_ADDRESS.ADDRESS_TYPE
FROM PATRON
INNER JOIN PATRON_ADDRESS ON PATRON.PATRON_ID = PATRON_ADDRESS.PATRON_ID
LEFT JOIN [SELECT * FROM PATRON_PHONE WHERE PATRON_PHONE.PHONE_TYPE = 4 ]. AS P_PHONE ON PATRON_ADDRESS.ADDRESS_ID = P_PHONE.ADDRESS_ID
WHERE PATRON_PHONE.ADDRESS_TYPE = 1
ORDER BY PATRON.patron_id
Access has some goofy syntax for subqueries. You could create a separate query for the P_Phone subquery instead but the results would (should?) be the same.

Related

IS it possible to inner join 3 tables for SQL?

I'm trying to fetch certain data by using a column that exists in all 3 tables, I have three tables
[Txn].[TxnPaymentResponse]
[Txn].[Txn]
[Txn].[TxnLineItem]
Right now if I run the query, I'm able to get the correct data only userID.
But I want to grab another piece of information (suppose that call column X) from the 3rd table (TxnLineItem). That column doesn't exist in the first 2 tables. In this scenario how could I perform inner join and show that piece of info in the query?
DECLARE #CompletedTransactionSince DATETIME2(7) = '2022-09-13 00:00:00.000000'
SELECT DISTINCT
t.UserID
FROM
[Txn].[Txn] T WITH(NOLOCK)
INNER JOIN
[Txn].[TxnPaymentResponse] TPR WITH(NOLOCK) ON T.[TxnID] = TPR.[TxnID]
WHERE
TPR.[PaymentResponseType] = 'FINAL'
AND TPR.[AuthorizedAmount] > CONVERT(DECIMAL(9,3), 0)
AND (#CompletedTransactionSince IS NULL OR
T.[CreatedOn] > #CompletedTransactionSince)
Results from my query:
UserID
C1671FDA-70A8-4C07-BBDF-ACD06ADD145F
Table 3:
TxnID
StandardProductCategory
6FE0D0D0-9959-41AA-9BF0-00000003DED8
Carwash
D1B0EA51-C476-488C-A140-0000C1C7D099
General
Suppose I'm doing inner join like
INNER JOIN
[Txn].[TxnLineItem] TXL WITH(NOLOCK) ON T.[TxnID] = [Txn].[TxnID],
But my I want to grab the X column that has the same transactionID. I want to display UserID that has a Carwash only. Not sure if it's possible to write another clause with an inner join.
As per "I want to display UserID that has a Carwash only", you need to exclude records.
Just add these EXISTS and NOT EXISTS clauses at the end of to query.
EXISTS part is filter users that has CarWash.
NOT EXISTS part is exclude users that has StandardProduct Category records other than Car Wash
......
AND (#CompletedTransactionSince IS NULL OR
T.[CreatedOn] > #CompletedTransactionSince)
AND EXISTS ( SELECT 1 FROM [Txn].[TxnLineItem] TXL
WHERE T.[TxnID] = [Txn].[TxnID]
AND TXL.StandardProductCategory='Carwash')
AND NOT EXISTS( SELECT 1 FROM [Txn].[TxnLineItem] TXL
WHERE T.[TxnID] = [Txn].[TxnID]
AND TXL.StandardProductCategory=<>'Carwash')

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:

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

WHERE clause and LEFT JOIN in SQL Server query

I have this query:
SELECT
EnrollmentID, MarketID
FROM
Contracts AS CO
LEFT JOIN
Customers AS C ON C.EnrollmentID = CO.BatchID AND MarketID = 'AB'
WHERE
C.EnrollmentID IS NULL
Here, I have a question that in this query is it possible that the query will verify data for MarketID = 'AB' in left join because as per WHERE condition?
I am getting result of EnrollmentIDs and MarketIDs are all NULL.
Note: The LEFT JOIN keyword returns all the rows from the left table (Contracts ), even if there are no matches in the right table (Customers ).
Now, if you want to select right table column and there are no matching data in the right table ,like.
SELECT CO.EnrollmentID, CO.MarketID ,C.Some_col
FROM Contracts AS CO
LEFT JOIN Customers AS C ON C.EnrollmentID = CO.BatchID
so, C.Some_col column will get all the null value for no matching rows in the right table.i think this is the reason why you are getting the null value for
MarketIDs and EnrollmentIDs.
hope, this help you.

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;