SumIF Access - Multiple tables - sql

I am following the question I asked here: SumIF in SQL Access
I have 3 tables [Broker] , [OPT], [TRS]. [Broker] have 2 fields BRKR_CODE, Status and [OPT] and [TRS] Have 5 same fields BRKR1, BRKR2, Date, COM_BRK1, COM_BRKR2
OPT.BRKR1, OPT.BRKR2, TRS.BRKR1, TRS.BRKR2 are linked to Broker.BRKR_CODE and can be equal.
I want to write this query.... If Broker.Status="Active" then Select Broker.BRKR_CODE
and then for each Broker.BRKR_CODE (for example "CB") do the Sum of OPT.COM_BRKR1 when OPT.BRKR1="CB" + Sum of OPT.COM_BRKR2 when OPT.BRKR2="CB" and do the Sum of TRS.COM_BRKR1 when TRS.BRKR1="CB" + Sum of TRS.COM_BRKR2 when TRS.BRKR2="CB"
I wrote this code using the answer I got in the previous question... but still have syntax issue... basically I want to have an array with 3 column BRKR_CODE, OPT_Tot, TRS_Tot
SELECT Broker.BRKR_CODE,
Sum(OPT.COM_BRKR1)+ Sum(OPT.COM_BRKR2) AS OPT_Tot,
Sum(TRS.COM_BRKR1)+ Sum(TRS.COM_BRKR2) AS TRS_Tot
FROM Broker
INNER JOIN OPT
ON (Broker.BRKR_CODE = OPT.BRKR2) OR (Broker.BRKR_CODE = OPT.BRKR1)
INNER JOIN TRS
ON (Broker.BRKR_CODE = TRS.BRKR2) OR (Broker.BRKR_CODE = TRS.BRKR1)
WHERE Broker.Status = "Active"
GROUP BY Broker.BRKR_CODE

You should try to make this query in the designer (just to get the base joins right). You'll see Access does some different stuff with parentheses that other SQL engines don't do.
You need to wrap each set of joins in parentheses
SELECT Broker.BRKR_CODE,
Sum(OPT.COM_BRKR1)+ Sum(OPT.COM_BRKR2) AS OPT_Tot,
Sum(TRS.COM_BRKR1)+ Sum(TRS.COM_BRKR2) AS TRS_Tot
FROM (Broker
INNER JOIN OPT
ON (Broker.BRKR_CODE = OPT.BRKR2) OR (Broker.BRKR_CODE = OPT.BRKR1))
INNER JOIN TRS
ON (Broker.BRKR_CODE = TRS.BRKR2) OR (Broker.BRKR_CODE = TRS.BRKR1)
WHERE Broker.Status = "Active"
GROUP BY Broker.BRKR_CODE

Related

How can I do a SQL join to get a value 4 tables farther from the value provided?

My title is probably not very clear, so I made a little schema to explain what I'm trying to achieve. The xxxx_uid labels are foreign keys linking two tables.
Goal: Retrieve a column from the grids table by giving a proj_uid value.
I'm not very good with SQL joins and I don't know how to build a single query that will achieve that.
Actually, I'm doing 3 queries to perform the operation:
1) This gives me a res_uid to work with:
select res_uid from results where results.proj_uid = VALUE order by res_uid asc limit 1"
2) This gives me a rec_uid to work with:
select rec_uid from receptor_results
inner join results on results.res_uid = receptor_results.res_uid
where receptor_results.res_uid = res_uid_VALUE order by rec_uid asc limit 1
3) Get the grid column I want from the grids table:
select grid_name from grids
inner join receptors on receptors.grid_uid = grids.grid_uid
where receptors.rec_uid = rec_uid_VALUE;
Is it possible to perform a single SQL that will give me the same results the 3 I'm actually doing ?
You're not limited to one JOIN in a query:
select grids.grid_name
from grids
inner join receptors
on receptors.grid_uid = grids.grid_uid
inner join receptor_results
on receptor_results.rec_uid = receptors.rec_uid
inner join results
on results.res_uid = receptor_results.res_uid
where results.proj_uid = VALUE;
select g.grid_name
from results r
join resceptor_results rr on r.res_uid = rr.res_uid
join receptors rec on rec.rec_uid = rr.rec_uid
join grids g on g.grid_uid = rec.grid_uid
where r.proj_uid = VALUE
a small note about names, typically in sql the table is named for a single item not the group. thus "result" not "results" and "receptor" not "receptors" etc. As you work with sql this will make sense and names like you have will seem strange. Also, one less character to type!

Querydsl: how to make left join by column

Im trying to match this SQL query in querydsl
SELECT tr.* FROM test.TRIP_REQ tr left outer join test.ADDR_BOOK ab on tr.REQ_USERID=ab.USER_ID
I know how to make left join query if you join into identity column but struggle to make it work with joining on 2 alternative columns. tr.REQ_USERID and ab.USER_ID are not identity columns
This is my querydsl:
QTripReq qTripReq = QTripReq.tripReq;
QAddressBook qABook = QAddressBook.addressBook;
JPAQuery query = new JPAQuery(entityManager);
query.from(qTripReq).leftJoin(qABook).on(qTripReq.requestorUser.id.eq(qABook.user.id)).list(qTripReq);
This throws error:
Path expected for join! [select tripReq from com.TripReq tripReq left join ADDR_BOOK addressBook with tripReq.requestorUser.id = addressBook.user.id where tripReq.assignedCompany.id = ?1]
You need to add a target entity path to leftJoin(), so that
QTripReq qTripReq = QTripReq.tripReq;
QAddressBook qABook = QAddressBook.addressBook;
JPAQuery query = new JPAQuery(entityManager);
query.from(qTripReq).leftJoin(qTripReq.addressBook, qABook).on(qTripReq.requestorUser.id.eq(qABook.user.id)).list(qTripReq);
Take a look on Using joins section in docs.

VB.NET Reportviewer How to make Inner Join work properly

I have 2 tables
tableAssembly, with columns SerialNumber, Brick1SerialNumber, Brick2SerialNumber,DateInserted
tableBricks, with columns SerialNumber, Lot, Weight, DateMeasured
In VB.NET(WinForms) I have been able to get the reportviewer control to work and print out information from both tables and also to enable/disable columns, and apply filters such as LIKE
In case it isn't clear tableAssembly.Brick1SerialNumber = tableBricks.SerialNumber
What I now want to do is when a user prints out a report from tableAssembly, I want them to be able to filter based on Brick1SerialNumber.Lot or Brick2SerialNumber.Lot or Brick1SerialNumber.DateMeasured or Brick2SerialNumber.DateMeasured
I understand I need to INNER JOIN tableAssembly.Brick1SerialNumber = tableBricks.SerialNumber AND tableAssembly.Brick2SerialNumber = tableBricks.SerialNumber
Do I also need to INNER JOIN the other columns from tableBricks to columns in tableAssembly? or does the INNER JOIN of Brick1SerialNumber = SerialNumber and Brick2SerialNumber = Serial make it so I can filter based on .Lot?
There are two ways to approach this. 1) join to tableBricks twice (probably not necessary for this condition) or attach it once, using both foreign keys (works for this condition, but not for all conditions).
Try this query:
SELECT tableAssembly.*
FROM tableAssembly INNER JOIN tableBricks
ON tableAssembly.Brick1SerialNumber = tableBricks.SerialNumber
OR tableAssembly.Brick2SerialNumber = tableBricks.SerialNumber
WHERE tableBricks.Lot = 99 --actually means Brick1.Lot or Brick2.Lot
AND tableBricks.DateMeasured = '1/1/2000'
If you need a specific Lot or DateMeasured for Brick1 and Brick2 (not the same values), then try this query:
SELECT tableAssembly.*
FROM tableAssembly INNER JOIN tableBricks AS tableBricks1
ON tableAssembly.Brick1SerialNumber = tableBricks1.SerialNumber
INNER JOIN tableBricks AS tableBricks2
ON tableAssembly.Brick2SerialNumber = tableBricks2.SerialNumber
WHERE tableBricks1.Lot = 98
AND tableBricks2.Lot = 99
AND tableBricks1.DateMeasured = '1/1/2000'
AND tableBricks2.DateMeasured = '1/2/2000'

SumIF in SQL Access

Hello I am a beginner in VBA/SQL. I have 2 tables [Broker] and [OPT]. [Broker] have 2 fields BRKR_CODE, Status and [OPT] Have 5 fields BRKR1, BRKR2, Date, COM_BRK1, COM_BRKR2
OPT.BRKR1 and OPT.BRKR2 are linked to Broker.BRKR_CODE and can be equal.
I want to write this query.... If Broker.Status="Active" then Select Broker.BRKR_CODE
and then for each Broker.BRKR_CODE (for example "CB") do the Sum of OPT.COM_BRKR1 when OPT.BRKR1="CB" + Sum of OPT.COM_BRKR2 when OPT.BRKR2="CB"
I wrote this code.... but it doesnot work at all... as I said I am a newbie...
SELECT IIF(Broker.Status="Active",Broker.BRKR_CODE), Sum(OPT.COM_BRKR1)+ Sum(OPT.COM_BRKR2) AS OPT_Tot
FROM Broker INNER JOIN OPT ON (Broker.BRKR_CODE = OPT.BRKR2) AND (Broker.BRKR_CODE = OPT.BRKR1)
GROUP BY Broker.BRKR_CODE;
Thanks a lot
If I understand correctly, you need to change two things in your query. The first is to move the condition to the where clause. You seem to only want "Active" things. The second is to change the AND in the on clause to OR. You want matches on either code.
SELECT Broker.BRKR_CODE,
Sum(OPT.COM_BRKR1)+ Sum(OPT.COM_BRKR2) AS OPT_Tot
FROM Broker INNER JOIN
OPT
ON (Broker.BRKR_CODE = OPT.BRKR2) OR (Broker.BRKR_CODE = OPT.BRKR1)
WHERE Broker.Status = "Active"
GROUP BY Broker.BRKR_CODE;

SQL - Query Select Joins through multiple tables

Here is the situation:
I search for Persons with ID (empr.empr_cb)
that has bill(s) to pay (transactions.montant)
this refers to transactions.compte_id
which is identical to comptes.id_compte
this refers to comptes.proprio.id
which is identical to empr.id_empr
that would give us the Person ID (empr.empr_cb)
I tried this, but I don't know what joins to set (cross Join?):
SELECT `empr`.`empr_cb`,`transactions`.`montant`
FROM `empr`,`comptes`,`transactions`
WHERE `transactions`.`montant` > `0`
AND `transactions`.`encaissement` = `0`
AND `transactions`.compte_id` = `comptes`.`id_compte`
AND `comptes`.`proprio_id` = `id_empr`
Any ideas how to put the joins?
This query is already using implicit INNER JOINs. It can be rewritten this way:
SELECT empr.empr_cb
, transactions.montant
FROM empr
JOIN comptes ON comptes.proprio_id = empr.id_empr
JOIN transactions ON transactions.compte_id = comptes.id_compte
WHERE transactions.encaissement = 0
AND transactions.montant > 0