I have 2 tables in an MS Access 2013 database that I have pulled through an ODBC. One is called PUBLIC_PHONE_NUMBERS and the 2nd is PUBLIC_CUSTOMER. Now I need to join the two fields PHONE_ACCT and PHONE_NUMBER from the phone table to the customer table. The phone account number is the same as the customer account number, but I need each account to have its phone number listed on the customer's table as well.
Here is what I thought would work:
SELECT public_phone_numbers.phone_acct, public_phone_numbers.phone_number
FROM public_phone_numbers
INNER JOIN public_customer
ON public_phone_numbers.phone_acct = public_customer.c_acct;
not sure I understand what you need, but perhaps it's only a left outer join?
SELECT c.*, p.phone_number
FROM public_customer c
left outer JOIN public_phone_numbers p
ON c.c_acct = p.phone_acct
Related
I need to join two dummy tables(one for actual amount and one for budgeted amounts) with Account table which have All account codes and Project Table which have all project Codes,
here my query,
TblActual :=select Actual."AcctCode",Actual."Project",sum(Actual."DocTotal")
from Actual
group by Actual."AcctCode",Actual."Project";
TblBuget := Select Budget."AcctCode",Budget."PrjCode",Sum(Budget."DocTotal")
Form Budget
Group by Budget."AcctCode",Budget."PrjCode";
Select
Project."PrjCode",Account."AcctCode",Account."AcctName",
:TblActual."Amount" as "Actual",:TblBuget."Amount" as "Budget"
From Account
Full outer Join :TblActual on Account."AcctCode" = :TblActual."AcctCode"
Full outer Join :TblBuget on Account."AcctCode" = :TblBuget."AcctCode"
Left Join Project on (:TblActual."Project"= Project."PrjCode" and :TblBuget."PrjCode" = Project."PrjCode" )
where Project."PrjCode"= xxx;
If I need to filter by a project I only received Accounts which both tables have the project and account code. But there are some accounts in budget table with this project code but not in the actual table and
Some accounts are in Actual table with this Project code and not in Budget table.
I need to get all this data without duplicating the account code.How to do that?
I want to join 5 tables to come up with 3 different result sets.
My tables are:
Customers - contains email address (unique id), email address domain name, account name, demographic info, etc (Customers)
Product 1 customer account names (Product1Accounts)
Product 1 customer email address domain names (Product1Domains)
Product 2 customer account names (Product2Accounts)
Product 2 customer email address domain names (Product2Domains)
The 3 results I need are:
Product 1 customers excluding Product 2 customers
Product 2 customers excluding Product 1 customers
Customers of both Product 1 and 2
Side Note: a customer of each product could be identified by account OR domain name.
I can create the following to come up with matches on table 1,2,3 or 1,4,5 but I am getting hung up on how to incorporate the exclusions
SELECT *
FROM Customers
INNER JOIN Product1Accounts
ON Customers.Company=Product1Accounts.Account
UNION
SELECT *
FROM Customers
INNER JOIN Product1Domains
on Customers.Email_Address_Domain=Product1Domains.Domain
I'm also not sure how to obtain the 3rd result set I am looking for... any help or advice on how to write this code better would be appreciated.
For the third result you can just keep adding UNIONs to get the additional results for the Product2 tables (which really smells like the database needs to be normalized).
For the first two results the EXCEPT keyword should work. I don't work with SQL Express, so I'm not sure if it's available in that version of SQL Server, but check it out. Your code would look like:
(
SELECT C.Company
FROM Customers C
INNER JOIN Product1Accounts PA ON PA.Account = C.Company
UNION
SELECT C.Company
FROM Customers C
INNER JOIN Product1Domains PD ON PD.Domain = C.Email_Address_Domain
)
EXCEPT
(
SELECT C.Company
FROM Customers C
INNER JOIN Product2Accounts PA ON PA.Account = C.Company
UNION
SELECT C.Company
FROM Customers C
INNER JOIN Product2Domains PD ON PD.Domain = C.Email_Address_Domain
)
I think your database structure is not correct, you are having 2 different tables for the 2 products. Does it mean that if in future you have 5 more products, you will have 5 more tables?
I think its better to remove the product1 and product2 tables and create one product table, then you can have another table that can be called productType which have the information about the type of the product and with a Foreign Key its connected to your main product Table
Also does any of your product tables have a Foreign Key available in your customers table? from what I can see they name of the columns you are trying to do the join on are not really clear to be a foreign key.
I'm just starting in the SQL world, so I have a very noob question:
I have 2 tables:
clients (columns: client_id and name)
accounts (columns: account_id and client_id)
and I need to write a query that shows the accounts of all the clients.
But, the problem is that not all the clients have accounts, if the client doesn't have one: how can I show the client_id, the name and NULL for the account_id column?
This query should work:
SELECT *
FROM accounts
LEFT [OUTER] JOIN clients
ON accounts.client_id = clients.client_id;
if not try this one:
SELECT *
FROM accounts
LEFT [OUTER] JOIN clients
ON accounts.client_id = clients.client_id WHERE clients.client_id IS NOT NULL;
These are plain SQL queries, I mean they are not PL-SQL specific. LEFT [OUTER] JOIN will only returns the columns of accounts table. [OUTER] keyword is optional, it defers from database version to version. ON accounts.client_id = clients.client_id will match client_id columns in both tables. Lastly, WHERE clients.client_id IS NOT NULL part should prevent the rows with NULL values in client_id cells.
Useful link: https://www.techonthenet.com/oracle/joins.php
Try this query it returns the clients name client id and shows null to those client who has no accountid.
select clients.name, accounts.account_id from accounts left join clients on
accounts.clintid=clients.client_id
Hi guys I have a query like somethign below. I am using two inner join and I am select from two two but different coloums
the first inner join changes to see if any of the staffs shares a company vehicle with other staff.
The second inner join changes to see if the what staff level drivers what type of vechicle in the company.
select
van_col1, van_col2, admin_col3, admin_col4
from
user
INNER JOIN
admin
ON
user.van_col1=adim.admin_col3
INNER JOIN
user
ON
adim
adim.admin_col3=user.van_col2
If you join the same table twice then you have to use alias names to differ the joins of the table
select u1.van_col1, u2.van_col2, a.admin_col3, a.admin_col4
from user u1
INNER JOIN admin a ON u1.van_col1 = a.admin_col3
INNER JOIN user u2 ON a.admin_col3 = u2.van_col2
And if you have columns in that tables that are named equaly then you have to tell the DB from which table you like to take the column, otherwise it would be ambiguous.
Hi I have an SQL table which has two tables which make reference to the same foreign key in a separate table twice... something like
SALES table
idSales idClient1 idClient2
1 1 2
CLIENT table
idClient ClientName
1 Bob
2 Mick
I want to join the SALES table to the CLIENT table and return data as follows:
idSales idClientClientName1 idClientClientName2
1 Bob Mick
Can anyone help with the SQL for this? I'm getting ambiguous column name errors on my join.
Thank you
You need to basically join table Client on table Sales twice because there are two columns on table Sales that are dependent on table Client.
SELECT a.idSales,
b.ClientName ClientName1,
c.ClientName ClientName2
FROM Sales a
INNER JOIN Client b
ON a.idClient1 = b.idClient
INNER JOIN Client c
ON a.idClient2 = c.idClient
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
But when one of the columns or both columns are nullable, INNER JOIN will not give you all records from Sales because it will only select where it has atleast one match on the other table. Instead use LEFT JOIN.
I might add that in cases like this, I use table aliases that hint at what entity you are linking to in the joined table. If for example, the foreign keys were to an address table, and you had a work address, and a Home address, I would use tables aliases of h and w for the two joins. In your case, i.e.,
Selext s.idSales,
c1.ClientName ClientName1,
c2.ClientName ClientName2
From Sales s
Join Client c1
On c1.idClient = s.idClient1
Join Client c2
On c2.idClient = s.idClient2
For those beginner SQL folks who may see this question in the future, it's helpful to add in the AS words, it makes it clearer still:
SELECT
Sale.idSales,
c1.ClientName AS ClientName1,
c2.ClientName AS ClientName2
FROM
Sales AS Sale
INNER JOIN Client AS c1 ON Sale.idClient1 = c1.idClient
INNER JOIN Client AS c2 ON Sale.idClient2 = c2.idClient