How do I join on a function in MS Access? - sql

I am trying to add a function to my JOIN condition in an MS Access query.
As a simplified example, consider that I have:
tblChildand tblParent and tblGrandparent
tblChild has both a nullable ParentId and GrandparentId depending on which one is known. I'm trying to write a query to take the more specific relationship provided. If this were TSQL it would be:
SELECT
columns
FROM
tblChild AS c
LEFT OUTER JOIN
tblParent AS p
ON p.[Id] = c.[ParentId]
LEFT OUTER JOIN
tblGrandparent AS gp
ON gp.[Id] = COALESE(p.[GrandParentId], c.[GrandParentId])
but I can't figure out how to get a COALESCE style function into the JOIN condition in Access. Here is what I would have expected to work:
SELECT
columns
FROM
(tblChild AS c
LEFT OUTER JOIN
tblParent AS p
ON p.[Id] = c.[ParentId])
LEFT OUTER JOIN
tblGrandparent AS gp
ON gp.[Id] = Nz(p.[GrandParentId], c.[GrandParentId])

I would expect NZ() to work. But can you use or instead?
SELECT columns
FROM (tblChild AS c LEFT JOIN
tblParent AS p
ON p.[Id] = c.[ParentId]
) LEFT JOIN
tblGrandparent AS gp
ON (gp.[Id] = p.[ParentId] or gp.[Id] = c.[GrandParentId]);
This isn't exactly the same. Formally, the last condition would be:
ON (gp.[Id] = p.[ParentId] or gp.[Id] = c.[GrandParentId] and gp.[Id] <> p.[ParentId]);
EDIT:
Another approach is two joins:
SELECT columns
FROM ((tblChild AS c LEFT JOIN
tblParent AS p
ON p.[Id] = c.[ParentId]
) LEFT JOIN
tblGrandparent AS gp
ON (gp.[Id] = p.[ParentId])
) LEFT JOIN
tblGrandparent AS gp2
ON gp.[Id] = c.[GrandParentId]);
You then have to adjust the "columns" to pull from either gp or gp2 as appropriate.

Related

Joining multiple tables ORACLE

SELECT *
FROM STORE
,MATERIAL
,STOREBIN
,VENDOR
,MATERIALPRICE
,SD
WHERE STOREBIN.STOREBINID(+) = SD.STOREBINID
AND SD.VENDORID = VENDOR.VENDORID(+)
AND MATERIAL.MATERIALID = MATERIALPRICE.MATERIALID(+)
AND STORE.STOREID = SD.STOREID
I have this simple query here, but i want to change the (+) notations into joins, how would this be possible? I was thinking:
RIGHT JOIN SD ON STOREBIN.STOREBINID = SD.STOREBINID
LEFT JOIN VENDOR ON VENDOR.VENDORID = SD.VENDORID
LEFT JOIN MATERIALPRICE ON MATERIALPRICE.MATERIAID = MATERIAL.MATERIALID
WHERE STORE.STOREID = SD.STOREID
But that hardly seems right because i need to join each table onto different tables.
This would be equivalent (if we also add the additional criteria you mentioned):
FROM
SD
INNER JOIN STORE as S
on S.STOREID = SD.STOREID
INNER JOIN MATERIAL as M
ON M.MATERIALID = SD.MATERIALID
LEFT JOIN STOREBIN as SB
ON SB.STOREBINID = SD.STOREBINID
LEFT JOIN VENDOR as V
ON V.VENDORID = SD.VENDORID
LEFT JOIN MATERIALPRICE as MP
ON MP.MATERIALID = M.MATERIALID

How do you work out the average of a sum function within a temp table?

I have created a temp table that lists each client's invoice(s), plus the number of days it took to pay the invoice. A client can have more than one invoice.
Instead of this, I would just like the temp table to list each client once, along with the AVERAGE number of days it took to pay all of their invoices.
Any tips on how to do this would be much appreciated.
Thanks
select
c.client_code,
b.bill_num,
b.bill_date,
ba.TRAN_DATE,
sum(datediff(Day,b.BILL_DATE, ba.TRAN_DATE)) as Days_To_Pay
into #tempG1
from blt_bill b
left outer join blt_billm bm on b.tran_uno = bm.bill_tran_uno
left outer join BLT_BILL_AMT ba on bm.BILLM_UNO = ba.BILLM_UNO
left outer join hbm_matter m on bm.matter_uno = m.matter_uno
left outer join hbm_client c on m.client_uno = c.client_uno
where b.total_bill_amt > 0.0
and bm.ar_status NOT IN ('P','X')
and ba.TRAN_TYPE in ('CR','crx')
group by c.client_code,b.bill_num,b.bill_date,ba.TRAN_DATE
select * from #tempG1
Drop Table #tempG1
I am not familiar with temp tables, but this should work (tested on a simliar scenario on MySQL8 and assuming that #tempG1 return results):
select
c.client_code,
b.bill_num,
b.bill_date,
ba.TRAN_DATE,
sum(datediff(Day,b.BILL_DATE, ba.TRAN_DATE)) as Days_To_Pay
from blt_bill b
left outer join blt_billm bm on b.tran_uno = bm.bill_tran_uno
left outer join BLT_BILL_AMT ba on bm.BILLM_UNO = ba.BILLM_UNO
left outer join hbm_matter m on bm.matter_uno = m.matter_uno
left outer join hbm_client c on m.client_uno = c.client_uno
where b.total_bill_amt > 0.0
and bm.ar_status NOT IN ('P','X')
and ba.TRAN_TYPE in ('CR','crx')
group by c.client_code,b.bill_num,b.bill_date,ba.TRAN_DATE
into #tempG1
############################
SELECT temp.client_code, AVG(temp.Days_To_Pay)
FROM (select * from #tempG1) as temp
GROUP BY temp.client_code
############################
#### Do you see results if drop? ####
Drop Table #tempG1
Note that I put #tempG1, at the bottom of your SELECT request, but might not be what want to achieve, not sure if you want to include your JOIN conditions or not.
Or you could do without temp table(including your join conditions):
SELECT temp.client_code, AVG(temp.Days_To_Pay)
(
select
c.client_code,
b.bill_num,
b.bill_date,
ba.TRAN_DATE,
sum(datediff(Day,b.BILL_DATE, ba.TRAN_DATE)) as Days_To_Pay
from blt_bill b
left outer join blt_billm bm on b.tran_uno = bm.bill_tran_uno
left outer join BLT_BILL_AMT ba on bm.BILLM_UNO = ba.BILLM_UNO
left outer join hbm_matter m on bm.matter_uno = m.matter_uno
left outer join hbm_client c on m.client_uno = c.client_uno
where b.total_bill_amt > 0.0
and bm.ar_status NOT IN ('P','X')
and ba.TRAN_TYPE in ('CR','crx')
group by c.client_code,b.bill_num,b.bill_date,ba.TRAN_DATE
) as temp
GROUP BY temp.client_code
This sounds like a simple aggregation:
select c.client_code, avg(datediff(Day, b.BILL_DATE, ba.TRAN_DATE)) as Days_To_Pay
from blt_bill b join
blt_billm bm
on b.tran_uno = bm.bill_tran_uno join
BLT_BILL_AMT ba
on bm.BILLM_UNO = ba.BILLM_UNO join
hbm_matter m on bm.matter_uno = m.matter_uno join
hbm_client c
on m.client_uno = c.client_uno
where b.total_bill_amt > 0.0 and
bm.ar_status not in ('P', 'X') and
ba.TRAN_TYPE in ('CR', 'crx')
group by c.client_code;
Note that you do not need outer joins. The where clause is turning most of them into inner joins anyway. Plus, if you are aggregating by the client code, then presumably you want a non-NULL value.

How can I avoid using the NOT IN and the SUBSELECT in the following query

How can I avoid using the NOT IN subselect in this query and also avoid the subselect ?
select idTipoDocumento,idDocumentoTarea
from ArchivosTarea as a
inner join Tarea as b on a.idEstadoTarea=b.idTarea
where b.idTarea = 160
and idDocumentoTarea not in (select idDocumentoTarea
from ArchivosTarea as a
inner join tiposArchivos as b on a.idTipoDocumento = b.idTipoArchivo
inner join documentoSolicitud as c on b.idTipoArchivo = c.Id_tipo_archivo
inner join tarea as d on a.idEstadoTarea=d.idTarea
where d.idTarea = 160)
I know that probably a LEFT JOIN or something like that should do the trick but I have tried that and it does not provide me the same results as this query.
The actual idea is to avoid the SUBSELECT (of the WHERE) and also avoid the NOT IN.
Using left join:
Select A.idTipoDocumento,A.idDocumentoTarea from
(select idTipoDocumento,idDocumentoTarea
from ArchivosTarea as a
inner join Tarea as b on a.idEstadoTarea=b.idTarea
where b.idTarea = 160)A
left outer join
(select idDocumentoTarea from ArchivosTarea as a
inner join tiposArchivos as b on a.idTipoDocumento = b.idTipoArchivo
inner join documentoSolicitud as c on b.idTipoArchivo = c.Id_tipo_archivo
inner join tarea as d on a.idEstadoTarea=d.idTarea
where d.idTarea = 160)B
on A.idDocumentoTarea=B.idDocumentoTarea
where B.idDocumentoTarea is null
It happens that SQL Server has antijoin aka semiminus aka EXCEPTION JOIN. It return rows in the left argument that don't match when joined by the right.
select idTipoDocumento,idDocumentoTarea
from ArchivosTarea as a
inner join Tarea as b on a.idEstadoTarea=b.idTarea
and b.idTarea = 160
exception join (
select idDocumentoTarea
from ArchivosTarea as a
inner join tiposArchivos as b on a.idTipoDocumento = b.idTipoArchivo
inner join documentoSolicitud as c on b.idTipoArchivo = c.Id_tipo_archivo
inner join tarea as d on a.idEstadoTarea=d.idTarea
where d.idTarea = 160)
You can do this with LEFT JOIN. Also using NOT EXISTS.

SQL - Join issue

I have the following query:
SELECT rt.ID, rt.Name, rt.Rate, rt.Colour, vtb.ID AS 'vtbID', vtb.Value, rt.StdID
FROM Rates AS rt
LEFT OUTER JOIN VehicleTypeCostsBreakdown AS vtb ON rt.ID = vtb.RateID
LEFT OUTER JOIN VehicleTypeCostsDepots AS vtd ON vtd.ID = vtb.VehicleTypeDepotID AND vtd.DepotID = #DepotID AND vtd.VehicleTypeID = #VehicleTypeID
Basically, I want to select all 'rates' from Rates table, but if any references to a rate exists in the 'vtd' table, which has parameters that match #DepotID and #VehicleTypeID, I want to bring back the Value for that. If it doesn't have any referenced, I want it the 'vtb.Value' selection to be blank.
With the SQL above, it seems to always return a value for 'vtb.Value' value, even if the parameters are null. Am I missing something?
Try it this way. Basically, you'll LEFT JOIN to the derived table formed by the INNER JOIN between VehicleTypeCostsBreakdown and VehicleTypeCostsDepots. The INNER JOIN will only match when all of your conditions are true.
SELECT rt.ID, rt.Name, rt.Rate, rt.Colour, vtb.ID AS 'vtbID', vtb.Value, rt.StdID
FROM Rates AS rt
LEFT OUTER JOIN VehicleTypeCostsBreakdown AS vtb
INNER JOIN VehicleTypeCostsDepots AS vtd
ON vtd.ID = vtb.VehicleTypeDepotID
AND vtd.DepotID = #DepotID
AND vtd.VehicleTypeID = #VehicleTypeID
ON rt.ID = vtb.RateID
Try:
SELECT rt.ID, rt.Name, rt.Rate, rt.Colour, vtb.ID AS 'vtbID', vtb.Value, rt.StdID
FROM Rates AS rt
LEFT OUTER JOIN (SELECT b.ID, b.Value, b.RateID
FROM VehicleTypeCostsBreakdown AS b
JOIN VehicleTypeCostsDepots AS d
ON d.ID = b.VehicleTypeDepotID AND
d.DepotID = #DepotID AND
d.VehicleTypeID = #VehicleTypeID)
AS vtb ON rt.ID = vtb.RateID
Try this:
SELECT rt.ID, rt.Name, rt.Rate, rt.Colour, vtb.ID AS 'vtbID', vtb.Value, rt.StdID
FROM Rates AS rt
LEFT JOIN VehicleTypeCostsBreakdown AS vtb ON rt.ID = vtb.RateID
LEFT JOIN VehicleTypeCostsDepots AS vtd ON vtd.ID = vtb.VehicleTypeDepotID
WHERE vtd.ID IS NULL OR (vtd.DepotID = #DepotID AND vtd.VehicleTypeID = #VehicleTypeID)
You don't need to specify that the LEFT JOIN is an OUTER JOIN and you shouldn't put conditions in the ON section of a JOIN, that's what WHERE is for.

Sql problem with my query

SELECT
dbo.pi_employee.emp_firstname, dbo.pi_employee.emp_lastname,
dbo.pi_employee.emp_no, dbo.pi_employee.emp_cnic,
dbo.pi_employee.emp_currentadd, dbo.pi_employee.emp_cellph,
dbo.pi_employee.emp_birthday, pi_jobtitle_1.jobtitle_name,
dbo.pi_employee.emp_joindate, dbo.pi_education.edu_degree,
dbo.pi_education.edu_year, dbo.pi_employee.emp_pension,
dbo.pi_employee.emp_age, dbo.pi_employee.emp_service,
dbo.pi_employee.emp_terminate, dbo.pi_employee.emp_termdate,
dbo.pi_employee.emp_basicofpay, dbo.pi_employee.emp_terminationreason,
dbo.pi_employee.emp_terminationdate, dbo.pi_employee.emp_status,
dbo.pi_employee.emp_gender, dbo.pi_employee.emp_maritalstatus,
dbo.pi_employee.emp_paymethod, dbo.pi_employee.emp_leaveentitle,
dbo.pi_employee.emp_confirmation, dbo.pi_employee.emp_title,
dbo.pi_employee.emp_basicamount, dbo.pi_salgrade.salgrade_name,
dbo.tbl_emp_status.StatusName, dbo.pi_skills.skill_type,
dbo.pi_location.loc_name, pi_location_1.loc_name AS wcity,
dbo.pi_jobtitlehist.jthSaleGradetype, dbo.pi_workexp.exp_serperiod,
dbo.pi_employee.emp_domicile, dbo.pi_skills.skill_type AS Skill,
dbo.pi_skills.skill_exp, dbo.pi_education.edu_degree AS Degree,
dbo.pi_education.edu_uni, dbo.pi_education.edu_distinction,
dbo.pi_lochistory.lhstart_date, dbo.pi_lochistory.lhend_date
FROM
dbo.pi_location
RIGHT OUTER JOIN
dbo.pi_workexp
RIGHT OUTER JOIN
dbo.pi_employee ON dbo.pi_workexp.emp_no = dbo.pi_employee.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitlehist ON dbo.pi_employee.emp_no = dbo.pi_jobtitlehist.emp_no ON
dbo.pi_location.loc_id = dbo.pi_employee.emp_location_id
LEFT OUTER JOIN
dbo.pi_salgrade ON dbo.pi_employee.emp_salgrade_id = dbo.pi_salgrade.salgrade_id
LEFT OUTER JOIN
dbo.tbl_emp_status ON dbo.pi_employee.emp_status = dbo.tbl_emp_status.StatusID
LEFT OUTER JOIN
dbo.pi_skills ON dbo.pi_employee.emp_no = dbo.pi_skills.emp_no
LEFT OUTER JOIN
dbo.pi_location AS pi_location_1
INNER JOIN
dbo.pi_lochistory ON pi_location_1.loc_id = dbo.pi_lochistory.loc_id ON dbo.pi_employee.emp_no = dbo.pi_lochistory.emp_no
LEFT OUTER JOIN
dbo.pi_education ON dbo.pi_employee.emp_no = dbo.pi_education.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitle AS pi_jobtitle_1 ON dbo.pi_employee.emp_jobtitle_id = pi_jobtitle_1.jobtitle_id
I am writing sql query to implement different scenario, but problem is that it gives repeated values. I write distinct and order by too but result was same can any one help me to solve this issue.
EDIT – The same query with table names aliased:
SELECT
em.emp_firstname, em.emp_lastname,
em.emp_no, em.emp_cnic,
em.emp_currentadd, em.emp_cellph,
em.emp_birthday, jt.jobtitle_name,
em.emp_joindate, ed.edu_degree,
ed.edu_year, em.emp_pension,
em.emp_age, em.emp_service,
em.emp_terminate, em.emp_termdate,
em.emp_basicofpay, em.emp_terminationreason,
em.emp_terminationdate, em.emp_status,
em.emp_gender, em.emp_maritalstatus,
em.emp_paymethod, em.emp_leaveentitle,
em.emp_confirmation, em.emp_title,
em.emp_basicamount, sg.salgrade_name,
es.StatusName, s.skill_type,
L.loc_name, L1.loc_name AS wcity,
jh.jthSaleGradetype, we.exp_serperiod,
em.emp_domicile, s.skill_type AS Skill,
s.skill_exp, ed.edu_degree AS Degree,
ed.edu_uni, ed.edu_distinction,
Lh.lhstart_date, Lh.lhend_date
FROM
dbo.pi_location AS L
RIGHT OUTER JOIN
dbo.pi_workexp AS we
RIGHT OUTER JOIN
dbo.pi_employee AS em ON we.emp_no = em.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitlehist jh ON em.emp_no = jh.emp_no ON L.loc_id = em.emp_location_id
LEFT OUTER JOIN
dbo.pi_salgrade AS sg ON em.emp_salgrade_id = sg.salgrade_id
LEFT OUTER JOIN
dbo.tbl_emp_status AS es ON em.emp_status = es.StatusID
LEFT OUTER JOIN
dbo.pi_skills AS s ON em.emp_no = s.emp_no
LEFT OUTER JOIN
dbo.pi_location AS L1
INNER JOIN
dbo.pi_lochistory AS Lh ON L1.loc_id = Lh.loc_id ON em.emp_no = Lh.emp_no
LEFT OUTER JOIN
dbo.pi_education ed ON em.emp_no = ed.emp_no
LEFT OUTER JOIN
dbo.pi_jobtitle AS j ON em.emp_jobtitle_id = j.jobtitle_id
Yeah, since you're doing a crap ton of Cartesian products with those joins without on clauses, that will cause plenty of what seem like 'repeated values', but are really just the product of the two tables combining sets.