Access is refusing to run an query with linked table? - sql

i have 3 tables each as follow
cash_credit
Bank_Name-------in_date-------Com_Id---Amount
America Bank 15/05/2010 1 200
HSBC 17/05/2010 3 500
Cheque_credit
Bank_Name-----Cheque_Number-----in_date-------Com_Id---Amount
America Bank 74835435-5435 15/05/2010 2 600
HSBC 41415454-2851 17/05/2010 5 100
Companies
com_id----Com_Name
1 Ebay
2 Google
3 Facebook
4 Amazon
Companies table is a linked table when i tried to create an query as follow
SELECT cash_credit.Amount, Companies.Com_Name, cheque_credit.Amount
FROM cheque_credit INNER JOIN (cash_credit INNER JOIN Companies ON cash_credit.com_id = Companies.com_id) ON cheque_credit.com_id = Companies.com_id;
I get an error saying that my inner Join is incorrectly, this query was created using Access 2007 query design
the error is
Type mismatch in expression
then i thought it might be the inner join so i tried Left Join and i get an error that this method is not used
JOIN expression is not supported
I am confused on where is the problem that is causing all this

Is the data type of column com_Id same/consistent across all the 3 tables?
If not, correct the data type and make it consistent for that column.
That should fix the issue for you.

I think you may want:
SELECT cash_credit.Amount, Companies.Com_Name, cheque_credit.Amount
FROM ( cheque_credit
INNER JOIN Companies
ON cheque_credit.com_id = Companies.com_id)
INNER JOIN cash_credit
ON cash_credit.com_id = Companies.com_id;

Regarding your first error:
Type mismatch in expression
This means that the types involved in some expression are not compatible with each other. Probably you are joining on two columns that have different types. Check the types of the following columns to ensure that they all have the same type:
Companies.com_id
cash_credit.com_id
cheque_credit.com_id
Regarding the different type of joins, as you seem to be uncertain when they should be used:
The difference between a LEFT JOIN and an INNER JOIN is if you have a row in the left table that doesn't match any row in the right table:
LEFT JOIN: returns the row with NULLs for the values in the right table
INNER JOIN: does not return the row

Related

Oracle Left outer joining same tables multiple times

I have four tables named as ROLE,EMPLOYEE, HIERARCHY_UNIT, EMPLOYEE_CLASS all of them have a different nameid column which is a primary key to STRING_TABLE table.Also STRING_TABLE will have a column stringtext that stores the exact text or we can say name.
All of these tables are linked as all of them will have a empid column.
Now i want to select some information from EMPLOYEE table along with the names of corresponding role, hierarchy_unit,employeeclass.My select query willl be something like
SELECT EMPST.STRINGTEXT,EROLE.STRINGTEXT,EHIER.STRINGTEXT,EEMPCLASS.STRINGTEXT
FROM EMPLOYEE EMP
INNER JOIN ROLE RO ON ROLE.EMPID=EMP.EMPID
INNER JOIN EMPLOYEE_CLASS EC ON EC.EMPID = EMP.EMPID
INNER JOIN HIERARCHY_UNIT HU ON HU.EMPID=EMP.EMPID
LEFT OUTER JOIN STRING_TABLE EMPST ON EMPST.NAMEID=EMP.NAMEID
LEFT OUTER JOIN STRING_TABLE EROLE ON RO.NAMEID=EROLE.NAMEID
LEFT OUTER JOIN STRING_TABLE EHIER ON HU.NAMEID=EHIER.NAMEID
LEFT OUTER JOIN STRING_TABLE EEMPCLASS ON EEMPCLASS.NAMEID=EC.NAMEID
The above query is working fine but i have a question whether doing the join to same table will not cause any performance issue. In the above example i have taken left outer join 3 times ( in actual i have a case of 26 joins with the string table ).Is there any way to optimize the above select query n and not to take join with same table multiple times?
It is hard to say anything about performance of a query without it's execution plan. Guessing is the only thing possible at the moment.
So, assuming STRING_TABLE.NAMEID is worth indexing (there are many unique values) and you already have this column indexed, this query is fine.
In the select-part of the query you've specified 4 STRING_TABLE's columns. This means you're asking the database to find values for different NAMEIDs from 4 different lines in that table and post them in one line for each line in the result query.
What the database has to do is looking 4 times (or 26 in production) for the particular line with particular nameid for each line in the output. This is why you need to join STRINGS_TABLE 4 times and, again, this is fine as long as column is worth-indexing and is already indexed.
If there are many non-unique data in NAMEID column, you might need to use partitioning or even to change database structure in order to get better performance. But, again, query is fine and I don't see any way to make it better

SQL query on Maria DB

i tried to figure out how to do the number 7 query on Helpdesk DB on SQLZOO (medium questions) but I can't. Not even using subquery or not exist statement. Here is the link (n. 7).
http://sqlzoo.net/wiki/Helpdesk_Medium_Questions
select first_name, last_name
from Caller a
left join Issue b
on a.Caller_id = b.Caller_id
where Call_date is null
This query says -
select ALL rows from Caller table regardless of them having a join
select ONLY rows from Issue where they have a join
Therefore, any row that returns from the Caller table that has not made a call will have a null value for Call_date. The where clause says only give me those rows.
You need to use LEFT JOIN on the Issue table and then add a filter where a field in the Issue table is null
SELECT ca.First_name, ca.Last_name
FROM Caller ca
LEFT JOIN Issue i ON ca.Caller_id = i.Caller_id
WHERE i.Caller_id is null

JOIN query to DBF via VBA

I apologize if this has been asked but i can't find where i'm going wrong here.
I need to query (2) dbf tables AP and VENDOR which contain vendors and payables. I need to get a list of all the payables entered between two specified dates. ap_vendor contains the vendor ID in the AP table and v_vendor contains the vendor ID in the vendor table.
I need to use a join to return the vendor name and the amount which are in separate tables.
This is my query:
SELECT a.ap_invamt, a.ap_invoice, b.v_name
FROM AP a JOIN VENDOR b
ON a.ap_vendor = b.v_vendor
I need to add a WHERE clause as well but i cant even get this part to run.
Keep getting error: "Syntax error in FROM clause"
Unlike other SQL dialects, you cannot use just the word JOIN to specify an inner join in Access (JET) SQL. You have to use both keywords: a INNER JOIN b.
Interestingly enough, I just tested it and JET does allow for LEFT JOIN and RIGHT JOIN, without the OUTER keyword.
Change your query to read FROM AP a INNER JOIN Vendor b and it should work.

Inconsistent Hive Left Join Results

I composed a simple left out join hiveql
select * from a left outer join b on (a.f1=b.f1 and a.f2=b.f2)
The total count of above query result is 798,608.
However, the total number of records in table a is 780,499, which doesn't match.
I tried to find all records that only exist in the left join results but not in table a; the results returned blank.
I even tried to create 2 small tables (a' and b') with a few records and the count of the left join result matches the count of table a' records, as expected.
What could cause the inconsistent results?
Thanks to David Lee. There are 1 to many situation in table b.
Problem solved.

Duplicate columns with inner Join

SELECT
dealing_record.*
,shares.*
,transaction_type.*
FROM
shares
INNER JOIN shares ON shares.share_ID = dealing_record.share_id
INNER JOIN transaction_type ON transaction_type.transaction_type_id = dealing_record.transaction_type_id;
The above SQL code produces the desired output but with a couple of duplicate columns. Also, with incomplete display of the column headers. When I change the
linesize 100
the headers shows but data displayed overlaps
I have checked through similar questions but I don't seem to get how to solve this.
You have duplicate columns, because, you're asking to the SQL engine for columns that they will show you the same data (with SELECT dealing_record.* and so on) , and then duplicates.
For example, the transaction_type.transaction_type_id column and the dealing_record.transaction_type_id column will have matching rows (otherwise you won't see anything with an INNER JOIN) and you will see those duplicates.
If you want to avoid this problem or, at least, to reduce the risk of having duplicates in your results, improve your query, using only the columns you really need, as #ConradFrix already said. An example would be this:
SELECT
dealing_record.Name
,shares.ID
,shares.Name
,transaction_type.Name
,transaction_type.ID
FROM
shares
INNER JOIN shares ON shares.share_ID = dealing_record.share_id
INNER JOIN transaction_type ON transaction_type.transaction_type_id = dealing_record.transaction_type_id;
Try to join shares with dealing_record, not shares again:
select dealing_record.*,
shares.*,
transaction_type.*
FROM shares inner join dealing_record on shares.share_ID = dealing_record.share_id
inner join transaction_type on transaction_type.transaction_type_id=
dealing_record.transaction_type_id;