How to use joins to cross check data from multiple tables - sql

I got a few tables which I need to query to crosscheck information.
All tables are in the same database.
First table is named Confirmedsitesv2 and contains a column named sites.
Sedond table is named Standardwithmail and contains three columns. sites, username and mail.
Third table named CDDump contains alot of columns but there is only three I am intrested in. Uid, employeenddate and company.
I need to query so I get site, username and mail based on the following criteria.
Site from standardwithmail matches site in confirmedsitesv2, username from standardwithmail matches uid in CDDUmp, while the employmentenddate = 0 and company is not like %test%. Mail should not be matched with anything as it only occurs in standardwithmail but will show for all the hits from the query.
So I tryed to get my head around this with left joins for nearly two hours.
Anyone experienced who can who can explain how I should use join in this situation? I done outer joins before for totaly different case and that worked easy and fine, but can't get my head around this even after alot of time on google.
If needed I can create some fake tables and upload pictures as I can't show the real data here.

Please try this:
SELECT cfrsites.sites standard.username,
standard.mail
FROM Confirmedsitesv2 cfrsites,
Standardwithmail standard,
CDDump cd
where cfrsites.sites = standard.site
and standard.username = cd.Uid
If you can edit your post, add the Table structure and add some insert statements that will be helpful

So why mail did not work was because it's in the the big table I only used for other information, I renamed that column in standardwithmail and I ran:
select site, username, mailaddress
from Standardwithmail
left join CDDump on CDDump.uid = dbo.Standardwithmail.Username where Site in (select Site from Confirmedsitesv2) and employmentEnd like '0' and companyName not like '%test%'
order by Site;

Try out the following query. You don't need outer join or left join, but just inner joins since you only want matching records.
SELECT swm.username,
swm.mail
FROM standardwithmail swm
INNER JOIN confirmedsitesv2 csv2
ON swm.sites = csv2.sites
INNER JOIN cddump cdd
ON swm.username = cdd.uid
WHERE cdd.employmentenddate = 0
AND
cdd.company NOT LIKE '‰test‰'

Related

Access SQL Lookup - Dropdown Columns

I have looked for a similar issue with no luck. Maybe I don't know the right term to search for.
This seems so simple, but I just can't get it after spending many hours trying different approaches.
I have a dropdown to select contracts which shows some ids for related fields. How can I get those IDs to show the value of another column.
SELECT tbl_contracts.ID, tbl_contracts.contract_name, tbl_contracts.firm_id, tbl_contracts.agency_id
FROM tbl_contracts;
image of dropdown
I would like the IDs shown for agency_id and firm_id to list the company_name from their respective table "tbl_firm_agencies" where the tbl_contracts looks them up from. I've tried INNER JOINS but when I do, I can only line items to show when both agency AND firm exist, so my dropdown get cut off quite a bit.
Simply LEFT JOIN to those lookup tables as opposed to INNER JOIN. Adjust table and field names to actual ones in query below. Also, the parentheses are required in MS Access SQL.
SELECT c.ID, c.contract_name, f.firm_name, a.agency_name
FROM (tbl_contracts c
LEFT JOIN tbl_firms f
ON c.firm_id = f.firm_name)
LEFT JOIN tbl_agencies a
ON c.agency_id = a.agency_name;

SQL Gather Data from the same column with possible different results

The problem I am currently experiencing is that if the Contact.email are different then it displays both as NULL. If I use OR instead of AND it acquires both of the emails but in different rows, so they are duplicated.
I think that I need to assign the alias but I'm not sure how to do that.
SELECT Item.brought, Contact.email AS SalesContact, Contact.email AS TechContact
FROM Customer
LEFT JOIN Contact
ON Item.sales_id = Contact_id
AND Item.tech_id = Contact_id;
What I am trying to do is associate the Item.brought with the two possible email address which as stored in a different table.
Any advice would be greatly predicated.
Many thanks.
You are reading the same email. You need to join twice to Contact, like this:
SELECT Item.brought, SC.email AS SalesContact, TC.email AS TechContact
FROM Customer
LEFT JOIN Contact SC
ON Item.sales_id = SC.Contact_id
LEFT JOIN Contact TC
ON Item.tech_id = TC.Contact_id;
Explanation: You need two Contact records, which are not necessarily the same, since you can imagine a tech who is not also on sales and a sales who is not also a tech. So you are joining the sales guy using Item.sales_id and the tech guy using Item.tech_id.

SQL join with references to references

I am still new to SQL. I have been making good progress on my project until I ran into this problem. I have tried to search the net for this kind of problem but I cannot find anything specific to this or I am not using the correct keywords in my search.
I have three tables relevant to this problem. Apparently I do not have any kind of "describe table" command to be able to copy the output. My other queries with joins are working as expected.
Table 1 - "Sites"
ID int auto-increment key, site_code short text, site_name short text, more but not relevant.
Table 2 - "Hubs"
ID int auto-increment key, HUB int (lookup from Sites.ID), more but not relevant.
Table 3 - "DialPlan"
ID int auto-increment key, site int (lookup from Sites.ID), HUB int (lookup from Hubs.HUB), more but not relevant.
When viewing the query for "DialPlan" I need to see "DialPlan.site" being replaced by "Sites.site_code" for that specific int. I need to see "DialPlan.HUB" being replaced by "Sites.site_code" for that specific int. Example of table output without joins:
DialPlan: 28, 29, 2, 203 That last number is not relevant.
Sites.ID = 29, Sites.site_name = BENN. Hubs.ID = 2, Hubs.HUB = 27, Sites.ID = 27, Sites.site_name = BRAG. So, the output I need to see when using the join is: 28, BENN, BRAG, 203. I am not getting that, I am getting: 28, BENN, BENN, 203.
My search query is:
select
Sites.site_code, Sites.site_name, Sites.site_code as Hubs.HUB,
DialPlan.OC
from
DialPlan
left join
Sites on DialPlan.site = Sites.ID
left join
Hubs on DialPlan.HUB = Hubs.ID
left join
Hubs on Hubs.HUB = Sites.ID;
I have tried to change field 3 using "AS" and even tried "=" and several other things. If I try to put field 3 as "Sites.site_code" then the output is the same as the first "Sites.site_code" lookup. I am not sure how to proceed. I have tried so many things now that I am not even sure exactly what I have tried. I saw one thread where there were multiple dots per column and I have no idea what that is used for. Does anyone have any ideas?
OK, I have found the answer and it is called an inner query. Actually there are two inner queries. I am posting this so that others may benefit from this. The problem with referencing the first table from two fields that are on the same table is that SQL cannot determine that the second call means a new search. So, you must perform an inner query to resolve this. Here is the code. As an Access admin I have always been able to get this kind result easy but I am behind the power curve for SQL. Here is the working code sample.
SELECT Sites.site_code, Sites.site_name,
( SELECT site_code FROM Sites WHERE ID =
( SELECT HUB FROM Hubs WHERE DialPlan.HUB = Hubs.ID )
),
DialPlan.OC
FROM DialPlan
LEFT JOIN Sites
ON DialPlan.site = Sites.ID;
The output is what I was looking for, "BENN Benning BRAG 203". I hope this helps someone.
When you use the AS clause you are just giving that column a title for your table. It will not fill in the data from your alias in place of the data you are aliasing.
If you want to list the data for Hubs.hub you should SELECT Hubs.hub and then give it the title you want using AS mytitle.
Also you dint need that last join as your tables are already joined.
Also, the describe command is DESC and it should work... DESC sites
select
Sites.site_code, Sites.site_name, Hubs.HUB as Site, DialPlan.OC
from
DialPlan
left join
Sites on DialPlan.site = Sites.ID
left join
Hubs on DialPlan.HUB = Hubs.ID

SQL Issue With Joins

I have looked at the examples and I think that the word Login is the problem. Is this an internal command word in SQL? Here is what I am trying to do.
Table 1 is Login and has LoginID and LoginName
Table 2 is LoginGroup and has LoginID and GroupID
I want to join the two so that I can get a list of LoginNames for a specific GroupID.
I have tried many different iterations and solutions on this and other sites, but cant seem to find the right combination. Here is my most recent attempt:
SELECT Login.LoginName AS login_name
FROM Login
INNER JOIN login on login.loginid = LoginGroup.LoginID
WHERE LoginGroup.LoginID = 9230
This produces:
Msg 1013, Level 16, State 1, Line 1
The objects "login" and "Login" in the FROM clause have the same exposed names. Use correlation names to distinguish them.
Can someone tell me what this needs to look like?
on your inner join you need to do:
FROM Login
INNER JOIN LoginGroup on Login.LoginID = LoginGroup.LoginID
you join tables two tables and then specify what columns in the tables to match on. It appears you were trying to join table straight to column.

How to show values from two different queries?

I have one database that contains all of user information including name. Then there is a second database that contains notes from the users and it contains the #id but not the name. The query i am doing to retrieve user notes doesn't have name so all its doing is showing the notes, then right under it i am doing another query to retrieve the name from the first database using the common #id. But it won't show.
Is there a way I can do this query in one? Please help. Thanks.
Use:
SELECT u.name,
n.*
FROM DB2.NOTES n
LEFT JOIN DB1.USERS u ON n.id = u.id
ORDER BY u.name
Assuming the connection credentials has access to both databases, you prefix the database name in front of the table name and separate with a period.
The LEFT JOIN will show both users, and notes without users associated. Here's a good primer on JOINs.
You might need to show your code, but you can write queries against two databases (or schemas) on the same host, just qualify the table names with the database name, e.g.
SELECT db1.user.id, db1.user.name, db2.userinfo.notes
FROM db1.user
INNER JOIN db2.userinfo ON(db1.user.id=db2.userinfo.id)
The credentials you are connecting with must have access to both databases for this to work of course.