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.
Related
Made this account just to ask about this question after being unable to find/expending the local resources I have, so I come to you all.
I'm trying to join two tables - ORDERS and CUSTOMER - as per a question on my assignment
For every order, list the order number and order date along with the customer number, last name, and first name of the customer who placed the order.
So I'm looking for the order number, date, customer number, and the full name of customers.
The code goes as such
SELECT ORDERS.ORDR_ORDER_NUMBER, ORDERS.ORDR_ORDER_DATE, ORDERS.ORDR_CUSTOMER_NUMBER, CUSTOMER.CUST_LAST, CUSTOMER.CUST_FIRST
FROM ORDERS, CUSTOMER
WHERE ORDERS.ORDR_CUSTOMER_NUMBER = CUSTOMER.CUST_CUSTOMER_NUMBER;
I've done this code without the table identifiers, putting quotation marks around ORDERS.ORDR_CUSTOMER_NUMBER, aliases for the two tables, and even putting a space after ORDR_ in both SELECT & WHERE for laughs and nothing's working. All of them keep coming up with the error in the title (ORA-00904), saying [ORDERS.]ORDR_CUSTOMER_NUMBER is the invalid identifier even though it shouldn't be.
Here also are the tables I'm working with, in case that context is needed for help.
Anyway, the query that produces the result you want should take the form:
select
o.ordr_order_number,
o.ordr_order_date,
c.cust_customer_number,
c.cust_last,
c.cust_first
from orders o
join customer c on c.cust_customer_number = o.ordr_customer_number
As you see the query becomes a lot easier to read and write if you use modern join syntax, and if you use table aliases (o and c).
You have to add JOIN or INNER JOIN to your query. Because the data comes from two different tables the WHERE clause will not select both.
FROM Orders INNER JOIN Customers ON Orders.order_Customer_Number = Customer.Cust_Customer_Number
My task: "Compile an SQL query that outputs a specific store (enter parameter window) the age of the youngest buyer"
I´ve tried some things, but because i´m new to SQL and i have no idea what i´m doing non of them seem to work.
I´d really appreciate, if someone would help me.
Thanks!
First you need to know the fields to SELECT (or return) and the table FROM which you are querying (asking) data; let's say you have the following tables: tblStores (containing a list of stores and related info), tblCustomers (containing customers and related info, e.g. ages, names, phone numbers, etc.), and tblPurchases (containing all the purchases at all stores by all customers and related info). You want the minimum age of a customer making a purchase at a specfic store, so you could use a MIN aggregating function. You would want to join (or relate) the tables based on customers and purchases. See my INNER JOINs in the example below. Then you filter the result by the user-inputted store name (inputStoreName) using WHERE; since the inputStoreName is undefined, in Access this would cause the parameter entry popup window to appear.
SELECT list of fields or aggregating functions you want (comma-separated)
FROM list of tables the fields are in (comma-separated) and how to join the tables
WHERE list of conditions to filter the data (separated by AND or OR)
Example:
SELECT tblStores.Name, tblStores.Description, MIN(tblCustomers.age)
FROM tblStores INNER JOIN ( tblPurchases INNER JOIN tblCustomers on tblPurchases.customerID = tblCustomer.customerID) ON tblStores.storeID = tblPurchases.storeID
WHERE (tblStores.Name = inputStoreName);
I recommend checking W3 schools. They are usually helpful for most programming tasks. If you provide more info about your database, we can provide more directed help.
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‰'
I have an UltraGrid displaying customer information in it. The way the database is set up, there are 2 tables. Customers and Customer_Addresses. I need to be able to display all of the columns from Customers as well as Town and Region from Customer_Addresses, but I'm under the impression that I'd need Town and Region columns in the Customer table to be able to do this? I've never used an INNER JOIN before so I'm not sure if this is true or not, so can anybody give me pointers on how to do this, or if I need the matching columns or not?
Does it even require an INNER JOIN, or is there an alternative way to do this?
Below are the design views of both of the tables - Is it possible to display Add4 and Add5 from Customer_Addresses with all of Customers?
As long as you have another key column you can use to link the tables (ex. ID_Column), it is better that you use LEFT JOIN.
Example:
SELECT c.col1, ... , c.colN, a.town, a.region FROM Customers c
LEFT JOIN Customer_Addresses a ON a.ID_Column = c.ID_Column
In order to clarify how JOIN types work, look at this picture:
In our case, using a LEFT JOIN will take all information from the Customers table, along with any found matching (on ID) information from Customer_Addresses table.
First of all you need some column in common in two tables, all what you have to do is:
CREATE TABLE all_things
AS
SELECT * (or columns that you want to have in the new table)
FROM Costumers AS a1
INNER JOIN Customer_Addresses AS a2 ON a1.column_in_common = a2.column_in_common
The point is what kind of join do you want.
If you can continue the process without having information in table Costumers or in table Customer_Addresses maybe you need OUTER JOIN or other kind of JOIN.
Is an outer join only used for analysis by the developer? I'm having trouble finding a use case for why you would want to include data in two or more tables that is unrelated or does not "match" your select criteria.
An example use case would be to produce a report that shows ALL customers and their purchases. That is, show even customers who have not purchased anything. If you do an ordinary join of customers and purchases, the report would show only those customers with at least one purchase.
Here is a very good list of examples by none other than Jeff Atwood - A Visual Explanation of SQL Joins he shows a new representation visually of the SQL joins.
The "doesn't match" case is useful for optional data. Something which is present for some rows, but not for others.
The OUTER JOIN is intended to address the cases where you wish to select a set of records from a primary table, which may or may not have related records contained in a secondary table.
An INNER join would omit from the list any primary records not also represented in the secondary table. Any OUTER JOIN guarantees the visiblity of all qualified primary records.
The difference between outer and inner joins is primarily that the outer joins retain each record from both the joined tables, whether the join predicate is matched or not. So a use case would need to revolve around knowing about "missing stuff", eg. customers who haven't made purchases, or members who have missing personal details.
Perhaps you have a users table and an address table related to it, since your users may choose not to enter an address, or they may have several (shipping, street, postal, etc). If you wanted to list all your users, but also display addresses for those that have them, you'd have to use an outer join for the address table.
Say you had two tables one for users and one for something like qualifications, you want to do a query that gets all users with their qualifications if they have any. In this case you would use an outer join to get all the users back and those with qualifications. An inner join would only give you the users who had a qualification.
A great article about outer joins:
Meet the experts: Terry Purcell on coding predicates in outer joins: A comparison of simple outer join constructs