MS Access Select Multiple Joins - c#

I'm trying to select by c# and odbc from the following tables.
LinkTab (FromDevID, FromPort, ToDevID, ToPort)
DevList (ID,DevName...)
The result should look like
FromDevName | FromPort | ToDevName | ToDevPort
i tried already the following statement:
SELECT dev1.DevName, lt.FromPort, dev2.DevName, lt.ToPort
FROM (LinkTab lt
INNER JOIN DevList dev1 ON lt.FromDevID = dev1.ID)
INNER JOIN devList dev2 ON lt.ToDevID = dev2.ID
and I couldn't get all records. I guess there is a mistake at my join condition.

Presumably, you need left join:
SELECT dev1.DevName, lt.FromPort, dev2.DevName, lt.ToPort
FROM (LinkTab lt LEFT JOIN
DevList dev1
ON lt.FromDevID = dev1.ID
) LEFT JOIN
devList dev2
ON lt.ToDevID = dev2.ID;
The reason your query would not return all the rows is because the devises may not always match. If this is the case, then INNER JOIN will filter out the rows with unmatched devices. A LEFT JOIN will keep all the rows in the first table, assigning NULL for the columns from the second and third tables.

Related

Missing operator in 3 table join statement [duplicate]

Syntax Error (missing Operator) in query expression 'tbl_employee.emp_id = tbl_netpay.emp_id INNER JOIN tbl_gross ON tbl_employee.emp_id = tbl_gross.emp_ID INNER JOIN tbl_tax ON tbl_employee.emp_id - tbl_tax.emp_ID'.
SELECT tbl_employee.emp_ID,
tbl_employee.emp_name,
tbl_gross.BasicSalary,
tbl_gross.totalOT,
tbl_netpay.totalGross,
tbl_tax.totalLate,
tbl_tax.allowance,
tbl_tax.SSS,
tbl_tax.PhilHealth,
tbl_tax.GSIS,
tbl_tax.HDMF,
tbl_netpay.totalDeduc,
tbl_netpay.emp_ti,
tbl_netpay.emp_wt,
tbl_netpay.emp_np
FROM tbl_employee
INNER JOIN tbl_netpay ON tbl_employee.emp_id = tbl_netpay.emp_id
INNER JOIN tbl_gross ON tbl_employee.emp_id = tbl_gross.emp_ID
INNER JOIN tbl_tax ON tbl_employee.emp_id = tbl_tax.emp_ID;
I always get the error above.
Access requires parentheses in the FROM clause for queries which include more than one join. Try it this way ...
FROM
((tbl_employee
INNER JOIN tbl_netpay
ON tbl_employee.emp_id = tbl_netpay.emp_id)
INNER JOIN tbl_gross
ON tbl_employee.emp_id = tbl_gross.emp_ID)
INNER JOIN tbl_tax
ON tbl_employee.emp_id = tbl_tax.emp_ID;
If possible, use the Access query designer to set up your joins. The designer will add parentheses as required to keep the db engine happy.
Thanks HansUp for your answer, it is very helpful and it works!
I found three patterns working in Access, yours is the best, because it works in all cases.
INNER JOIN, your variant. I will call it "closed set pattern".
It is possible to join more than two tables to the same table with good performance only with this pattern.
SELECT C_Name, cr.P_FirstName+" "+cr.P_SurName AS ClassRepresentativ, cr2.P_FirstName+" "+cr2.P_SurName AS ClassRepresentativ2nd
FROM
((class
INNER JOIN person AS cr
ON class.C_P_ClassRep=cr.P_Nr
)
INNER JOIN person AS cr2
ON class.C_P_ClassRep2nd=cr2.P_Nr
)
;
INNER JOIN "chained-set pattern"
SELECT C_Name, cr.P_FirstName+" "+cr.P_SurName AS ClassRepresentativ, cr2.P_FirstName+" "+cr2.P_SurName AS ClassRepresentativ2nd
FROM person AS cr
INNER JOIN ( class
INNER JOIN ( person AS cr2
) ON class.C_P_ClassRep2nd=cr2.P_Nr
) ON class.C_P_ClassRep=cr.P_Nr
;
CROSS JOIN with WHERE
SELECT C_Name, cr.P_FirstName+" "+cr.P_SurName AS ClassRepresentativ, cr2.P_FirstName+" "+cr2.P_SurName AS ClassRepresentativ2nd
FROM class, person AS cr, person AS cr2
WHERE class.C_P_ClassRep=cr.P_Nr AND class.C_P_ClassRep2nd=cr2.P_Nr
;

How to join tables and queries in C# with access database

this is how i join 2 tables and select form it:
OleDbDataAdapter DataA = new OleDbDataAdapter(#"Select tfr.FeedID, tf.FeedName, tfr.FeedQuantity, tf.DM
FROM tFeeds AS tf
INNER JOIN tFeedsRations AS tfr ON (tf.FeedID=tfr.FeedID)", Connection);
but what about adding a access query to this select command?
for example I want to add this statement to my select command:
Select qfq.FeedDMQuantites
From qFeeds_Quantities as qfq
what should I do?
Well add another JOIN condition to this table qFeeds_Quantities (assuming you have a relationship to this table or a common column among other table).
Assuming you have a common column like FeedID in this new table as well you can make another JOIN like
select tfr.FeedID, tf.FeedName, tfr.FeedQuantity,
tf.DM, qfq.FeedDMQuantites
FROM (tFeeds AS tf
INNER JOIN tFeedsRations AS tfr ON tf.FeedID = tfr.FeedID)
INNER JOIN qFeeds_Quantities as qfq ON tf.FeedID = qfq.FeedID;
If you want to include another JOIN then parenthesize like
FROM ((tFeeds AS tf
INNER JOIN tFeedsRations AS tfr ON tf.FeedID = tfr.FeedID)
INNER JOIN qFeeds_Quantities as qfq ON tf.FeedID = qfq.FeedID)
INNER JOIN BLAH AS bll ON bll.test = tf.test;

How to remove repeated rows in this Access query?

How can I get rid of duplicated rows in my MS access query?
SELECT pt.code,
resis.resis,
sense.sense
FROM (sense INNER JOIN pt ON sense.code = pt.code)
INNER JOIN resis ON pt.code = resis.code;

Getting most recent record based on datetime column SQL

I am trying to select the most recent record. Right now it is returning all records. Which makes since, because I have not put in a filter to get most recent, I am unsure of how to do that. Here is my code:
select
c.my_Name, a.my_Num, d.myBrand, a.order_bill, a.myDate
from
[table1] a
left join
[table2] b on a.[myCode] = b.[myCode]
left join
[table3] c on c.myTag = b.myTag
left join
[table4] d on a.[myHash] = d.[myHash]
where
c.[myName] = 'test name'
I am wanting to get most recent record from the table, I am guessing the most efficient way is to get most recent a.myDate.
I'd write
SELECT TOP 1
c.my_Name, a.my_Num, d.myBrand, a.order_bill, CONVERT(char(10), a.myDate,126)
FROM [table1] a
LEFT JOIN [table2] b on a.[myCode] = b.[myCode]
LEFT JOIN [table3] c on c.myTag=b.myTag
LEFT JOIN [table4] d on a.[myHash] = d.[myHash]
WHERE c.[myName] = 'test name'
ORDER BY a.myDate DESC
Something like this might help
select c.my_Name, a.my_Num, d.myBrand, a.order_bill, MAX(a.myDate)
from [table1] a
left join [table2] b on a.[myCode] = b.[myCode]
left join [table3] c on c.myTag=b.myTag
left join [table4] d on a.[myHash] = d.[myHash]
where c.[myName] = 'test name'
group by c.my_Name, a.my_Num, d.myBrand, a.order_bill
order by a.myDate DESC
This will grab distinct records with the greatest (most recent) a.myDate, grouped by your other elements.
Be aware of duplicates. Use your duplicate criteria keys in the select statement, or whatever elements you absolutely need to be distinct.

About the proper use of INNER JOIN

I've been having problems trying to retrieve data from my database to a DataTable in Microsoft Visual C#. I've been told it's because of improper use of INNER JOINs. The query (Fill method) is the one that follows.
SELECT Bordero.id AS id, Titulo.id AS id_titulo, Titulo.valor AS valor_titulo,
Sacado.nome AS nome_sacado, Cliente.nome AS nome_cliente, Sacado.documento,
Titulo.taxa_adm AS taxa_adm_titulo, Titulo.desagio AS desagio_titulo,
Titulo.liquido AS liquido_titulo,
(CASE Titulo.tipo
WHEN 'True' THEN 'Cheque'
ELSE 'Duplicata'
END) AS tipo, Titulo.dias, Titulo.codigo, Titulo.vencimento,
Titulo.data_base, Bordero.desagio AS desagio_bordero,
Bordero.taxa_adm AS taxa_adm_bordero, Bordero.liquido AS liquido_bordero,
Bordero.bruto, Bordero.duplicata, Bordero.desconto, Bordero.iss, Bordero.iof,
Bordero.cpmf, Bordero.pis, Bordero.cofins, Desconto.valor AS valor_desconto,
Desconto.descricao, Bordero.id_cliente
FROM Bordero
INNER JOIN Cliente ON Bordero.id_cliente = Cliente.id
INNER JOIN Titulo ON Bordero.id = Titulo.bordero_id
INNER JOIN Sacado ON Sacado.id = Titulo.sacado_id
INNER JOIN Desconto ON Cliente.id = Desconto.id_cliente
The database diagram looks like this:
http://i53.tinypic.com/t0g4qp.jpg
Any hints on what's wrong?
Without stating what your problem is that you're experiencing my guess would be that you may need to be using LEFT OUTER JOIN for some of the tables instead of all INNER JOIN.
When using an INNER JOIN in a query, the data that you're matching via the ON clause must be not null in both tables. Meaning that if the ID exists on the left table (the one in the FROM clause) there must be a matching record in the right table (the one you're joining in via the INNER JOIN). If the right table does not have a matching record, the entire result is dropped from the query.
By using a LEFT OUTER JOIN instead, you allow the right table to return NULL instead for each data row that doesn't match.
TABLE A TABLE B
ID | Name ID | Address
1 | Alice 1 | 123 ABC St.
2 | Bob 3 | 789 XYZ St.
3 | Cam
Using the above tables, if you were to do an FROM A INNER JOIN B ON A.ID = B.ID only rows 1 and 3 would be returned. If you were to do a FROM A LEFT OUTER JOIN B ON A.ID = B.ID all rows from A would be returned and B.Address would be null for #2.

Categories