Join Statements in Access SQL - sql

I'm new with SQL and how it works with Access, and I had a problem with a query only returning items from a specific set.
So according to the definitions I read a LEFT JOIN statement is supposed to return all records from the Left table then only specific records from the Right table. For some reason it will not return all records from my left table. Please see code below. It still somehow wants to only return records that have some kind of relationship. Any Ideas? I want all records from my left table ( tbl_historicOrg) to be returned then only specific records from the Right table.
SELECT tbl_historicOrg.NAME, tbl_historicOrg.Racf, tbl_historicOrg.STATUS, tbl_historicOrg.PCF, tbl_historicOrg.[Date Finalized], tbl_historicOrg.[Hist Month], import_data.SCHDLE_EXCPT_NM, import_data.HOURS
FROM tbl_historicOrg LEFT JOIN import_data ON tbl_historicOrg.Racf = import_data.RACF
WHERE (((import_data.SCHDLE_EXCPT_NM)="System Problems"));
Any suggestions or corrections would be greatly appreciated.

Try like below; get the filtered data and do a JOIN with that result set
SELECT tbl_historicOrg.NAME,
tbl_historicOrg.Racf,
tbl_historicOrg.STATUS,
tbl_historicOrg.PCF,
tbl_historicOrg.[Date Finalized],
tbl_historicOrg.[Hist Month],
tab.SCHDLE_EXCPT_NM,
tab.HOURS
FROM tbl_historicOrg
LEFT JOIN (
select SCHDLE_EXCPT_NM,
HOURS,
RACF
from import_data where SCHDLE_EXCPT_NM = "System Problems" ) as tab
ON tbl_historicOrg.Racf = tab.RACF;

Your query should be like below, Dont use 'WHERE' clause for second table, its filtering the data for 'import_data.SCHDLE_EXCPT_NM = "System Problems"'
SELECT tbl_historicOrg.NAME, tbl_historicOrg.Racf, tbl_historicOrg.STATUS,
tbl_historicOrg.PCF, tbl_historicOrg.[Date Finalized], tbl_historicOrg.[Hist Month],
import_data.SCHDLE_EXCPT_NM, import_data.HOURS
FROM tbl_historicOrg LEFT JOIN import_data ON tbl_historicOrg.Racf = import_data.RACF

Related

Right outer join with where clause not returning expected results

I have a SQL statement that works perfectly:
SELECT
DailyRequest.*
FROM
DailyRequest
RIGHT OUTER JOIN
Facilities ON FacilityID = Facilities.ID
This shows all the records in Facilities, as expected. However, I need to show only the records for a specified date, but still all of them from Facilities. I thought the SQL below would work, but it does not.
This SQL only shows the records from DailyRequest:
SELECT
DailyRequest.*
FROM
DailyRequest
RIGHT OUTER JOIN
Facilities ON FacilityID = Facilities.ID
WHERE
RequestDate = '7/21/2021'
The date seems to introduce the problem, but I'm not sure what I am doing wrong. I want to show all the records from the joined tables, even if they don't exist in DailyRequest.
Pretty sure you need to apply the date limitation in the join itself for it to properly effect results. Try:
SELECT
DailyRequest.*
from DailyRequest
right outer join Facilities on FacilityID = Facilities.ID
and DailyRequest.RequestDate = '7/21/2021'

Can I do a left join without returning the conditional columns?

New to SQL but I want to be able to optimize my query by bringing just the right amount of data. I am doing a left join on CS Rep Name and WE, which are two columns present in both tables. I find that if I don't bring in CS Rep Name and WE in the TECDR table, the query would error. Is there a workaround to this? Since it is a left join, I don't need redundant data.
SELECT *
FROM Tish_Email_CSAT_Dump AS TECD
LEFT JOIN (SELECT CS_Rep_Name,
Team_Leader,
Operations_Manager,
Tenure,
WE,
FileName
FROM Tish_Email_CSAT_Dump_Roster) AS TECDR
ON TECD.CS_Rep_Name = TECDR.CS_Rep_Name
AND TECD.WE = TECDR.WE
When you embed a SELECT inside a query in place of a table, the result of a select (projection) behave like a table visible only inside the query.
In your case, the join is the same as if there were a table called TECDR with the columns that you select. Hence, if you leave out some columns of Tish_Email_CSAT_Dump_Roster from your SELECT, these columns would not be available for joining or selection.
However, in your case this is unnecessary: all you need to do is joining to the underlying table, like this:
SELECT
TECD.*
, TECDR.Team_Leader
, TECDR.Operations_Manager
, TECDR.Tenure
, TECDR.FileName
FROM Tish_Email_CSAT_Dump AS TECD
LEFT JOIN Tish_Email_CSAT_Dump_Roster AS TECDR
ON TECD.CS_Rep_Name = TECDR.CS_Rep_Name AND TECD.WE = TECDR.WE
select
<place the columns you want here>
from
Tish_Email_CSAT_Dump as TECD
Left join Tish_Email_CSAT_Dump_Roster as TECDR
On TECD.CS_Rep_Name = TECDR.CS_Rep_Name and TECD.WE = TECDR.WE
Hope the following helps or else please share the query that errors:
select TECD.Column1, TECD.Column2, TECDR.Column1, TECDR.Column2
from Tish_Email_CSAT_Dump as TECD
Left join Tish_Email_CSAT_Dump_Roster as TECDR
On TECD.CS_Rep_Name = TECDR.CS_Rep_Name and TECD.WE = TECDR.WE

SQL ORACLE Joining two queries together as one?

thanks in advance for your help.
I have a set of tables like this.
Also, here is a semi-accurate ERD to see what's going on.
I need to list all donations made by individual Alumni, and businesses. I need the first/last name and IDs present.
The following code returns all I need about the Alumni donors:
SELECT DonationID, Donation.AlumniID, BusinessID_FK, DONATIONDATE, Value, Alumnus.FirstName, Alumnus.LastName
FROM DONATION
JOIN Alumnus
on Donation.AlumniID=Alumnus.alumniid;
And the following code returns all I need about the Business donors:
SELECT * FROM DONATION
JOIN BusinessSponser
on Donation.businessid_fk=businesssponser.businessid;
Furthermore, the following query returns all the IDs, however no First/Last names:
SELECT * FROM DONATION
LEFT JOIN BusinessSponser
on Donation.businessid_fk=businesssponser.businessid;
So my question is, how can I merge these queries together as one? My intention is to create a view that would display the final result.
Thanks in advance for your help.
The outer join is a SQL technique which allows us to join rows from one table with some rows from another table. In your case you have two optional tables so you need two left outer joins:
select donationid
, donation.donationdate
, donation.value
, donation.alumniid
, alumnus.firstname
, alumnus.lastname
, donation.businessid
, businesssponser.businessname
from donation
left outer join alumnus
on donation.alumniid=alumnus.alumniid;
left outer join businesssponser
on donation.businessid_fk=businesssponser.businessid;

SQL Lookup in MS Access Query

I have an Access Database with a table [Inventory] with following fields:
[Inventory].[Warehouse]
[Inventory].[PartNumber]
I also have a query [TransactionsQry] with following fielfd:
[TransactionsQry].[PartNumber]
[TransactionsQry].[SumofTransactions]
Now I would like to make a new query which shows all part numbers per Warehouse (from table [Inventory]) and look up related (number of) transactions in query [TransactionsQry]. Not every part number in [Inventory] has transactions yet and if so I would like to display "0".
At first I successfully tried this with a DLookup, but the result is a very slow query for very little data.
That is why I tried the following (but unsuccessfully displaying only matched part numbers and an additional error message):
SELECT
Inventory.Warehouse AS Warehouse,
TransactionsQry.PartNumber AS PartNumber,
TransactionsQry.SumofTransactions AS SumofTransactions
FROM Inventory
INNER JOIN TransactionsQry ON Inventory.PartNumber = TransactionsQry.PartNumber;
Any help with solving this issue in SQL is highly appreciated. Thanks.
You would be needing a LEFT JOIN based on what you need. Along with a Nz to treat Nulls as 0. Here is the corrected CODE
SELECT
Inventory.Warehouse AS Warehouse,
TransactionsQry.PartNumber AS PartNumber,
Nz(TransactionsQry.SumofTransactions, 0) AS SumofTransactions
FROM
Inventory LEFT JOIN TransactionsQry
ON
Inventory.PartNumber = TransactionsQry.PartNumber;
You want to use left join rather than inner join for this. Also, table aliases make queries easier to read and write:
SELECT i.Warehouse AS Warehouse,
tq.PartNumber AS PartNumber,
nz(tq.SumofTransactions, 0) AS SumofTransactions
FROM Inventory as i LEFT JOIN
TransactionsQry as tq
ON i.PartNumber = tq.PartNumber;
However, I'm guessing that you really want a group by:
SELECT i.Warehouse AS Warehouse,
tq.PartNumber AS PartNumber,
nz(sum(tq.SumofTransactions), 0) AS SumofTransactions
FROM Inventory as i LEFT JOIN
TransactionsQry as tq
ON i.PartNumber = tq.PartNumber
GROUP BY i.Warehouse, tq.PartNumber;

How to Select Specific data on query with RIGHT JOIN statement?

I am joining 2 tables using RIGHT JOIN statement. I used below query and it works good. However it still display all data whenever I tried to select specific user
SELECT TBLNOTIFICATIONS.NOTIFICATION_ID, TBLNOTIFICATIONS.NOTIFICATION_TYPE, FILENAMES_LIST.LOCATION_FILENAME, TBLNOTIFICATIONS.NOTIFICATION_DATE
FROM TBLNOTIFICATIONS
RIGHT JOIN FILENAMES_LIST
ON TBLNOTIFICATIONS.NOTIFICATION_ID=FILENAMES_LIST.NOTIFICATION_ID
WHERE TBLNOTIFICATIONS.USER_ID='JCON'
What should I do to select data from specific user?
Thanks in advance.
You are filtering on the left table, so all the data of the right table will still be shown.
It is probably enough to change the query to a LEFT JOIN to get the results you want.
Besides that, you can use aliases to make your query more readable, like so:
SELECT tn.NOTIFICATION_ID, tn.NOTIFICATION_TYPE, fl.LOCATION_FILENAME, tn.NOTIFICATION_DATE
FROM TBLNOTIFICATIONS AS tn
LEFT JOIN FILENAMES_LIST AS fl
ON tn.NOTIFICATION_ID = fl.NOTIFICATION_ID
WHERE tn.USER_ID='JCON'