How to do multiple Left join with multiple ids with just 1 row (Top 1) on the right table - sql

Im doing Multiple left joins on multiple Ids. Im trying to match only 1 result from the right table but not sure how to write the nested select when the join is on multiple IDs.
The last 2 left joins return more than 1 row even with the joins on multiple IDs (expected) however I'm only interested in matching ONE (Top 1) of the result on the right table.
I've already checked out other posts regarding left joins but none of them includes joining with multiple ids.
Basically I'm trying to get a list of operations from the Operation Table (Left Table) . Each operation has an identifier with the Type, Base ID, Lot ID, Split ID, Sub ID & Sequence No which lead to a unique operation. All the tables mentioned have each of these columns present.
However, the "Requirement" table "R" may have more than 1 row for each unique operation and I only need 1 requirement from it. Same is the case for the "Operation_Audit" table "OA".
I've only put 1 column for each table under the select for ease of reading.
Select
O.SETUP_HRS As [Setup Time], O.RUN_HRS As [Run Time],
OS.RESOURCE_ID As [Shop Resource],
WO.PART_ID As [Part ID / Assy],
P.DESCRIPTION As [Part / Assy Description],
WOV.DESCRIPTION As Description,
R.CLOSE_DATE As [Req Issue Date],
OA.RESULT_DETAIL As Reason
From OPERATION As O
Inner Join OPERATION_SCHED OS On
O.WORKORDER_TYPE = OS.WORKORDER_TYPE And
O.WORKORDER_BASE_ID = OS.WORKORDER_BASE_ID And
O.WORKORDER_LOT_ID = OS.WORKORDER_LOT_ID And
O.WORKORDER_SPLIT_ID = OS.WORKORDER_SPLIT_ID And
O.WORKORDER_SUB_ID = OS.WORKORDER_SUB_ID And
O.SEQUENCE_NO = OS.SEQUENCE_NO
Inner Join WORK_ORDER WO On
O.WORKORDER_TYPE = WO.[TYPE] And
O.WORKORDER_BASE_ID = WO.BASE_ID And
O.WORKORDER_LOT_ID = WO.LOT_ID And
O.WORKORDER_SPLIT_ID = WO.SPLIT_ID
Left Outer Join PART P On
P.ID = WO.PART_ID
Inner Join CR_WO_PART WOV On
O.WORKORDER_TYPE = WOV.[TYPE] And
O.WORKORDER_BASE_ID = WOV.BASE_ID And
O.WORKORDER_LOT_ID = WOV.LOT_ID And
O.WORKORDER_SPLIT_ID = WOV.SPLIT_ID And
O.WORKORDER_SUB_ID = WOV.SUB_ID
Left Outer Join REQUIREMENT R On
O.WORKORDER_TYPE = R.WORKORDER_TYPE And
O.WORKORDER_BASE_ID = R.WORKORDER_BASE_ID And
O.WORKORDER_LOT_ID = R.WORKORDER_LOT_ID And
O.WORKORDER_SPLIT_ID = R.WORKORDER_SPLIT_ID And
O.SEQUENCE_NO = R.OPERATION_SEQ_NO And
O.WORKORDER_SUB_ID = R.WORKORDER_SUB_ID
Left Outer Join OPERATION_AUDIT OA On
O.WORKORDER_TYPE = OA.WORKORDER_TYPE And
O.WORKORDER_BASE_ID = OA.WORKORDER_BASE_ID And
O.WORKORDER_LOT_ID = OA.WORKORDER_LOT_ID And
O.WORKORDER_SPLIT_ID = OA.WORKORDER_SPLIT_ID And
O.WORKORDER_SUB_ID = OA.WORKORDER_SUB_ID And
O.SEQUENCE_NO = OA.SEQUENCE_NO And
OA.RESULT = 'M'
Where
O.COMPLETED_QTY <= O.CALC_END_QTY And
WO.SUB_ID = '0' And
O.WORKORDER_TYPE = 'W' And
OS.SCHEDULE_ID = 'STANDARD' And
FilterCriteria = #FilterCriteria

I would create the queries that result in the desired one row being returned for each unique operation from tables R and OA. Then set those queries up as common table expressions (CTE). Then do your original query above (left is good enough, left outer not needed) but join to the single record CTE's instead of R and OA.

Related

Speed up SQL query performance with nested queries

Could anyone help me speed this query up? It currently take 17 minutes to run but does return the correct data and it populates a subform in MS Access. Functions in the rest of the VBA are declared as long to try to speed up more.
Here's the full query:
SELECT lots of things
FROM (((((((((((((((ngstest
INNER JOIN patients
ON ngstest.internalpatientid = patients.internalpatientid)
INNER JOIN referral
ON ngstest.referralid = referral.referralid)
INNER JOIN checker
ON ngstest.bookby = checker.check1id)
INNER JOIN ngspanel
ON ngstest.ngspanelid = ngspanel.ngspanelid)
LEFT JOIN ngspanel AS ngspanel_1
ON ngstest.ngspanelid_b = ngspanel_1.ngspanelid)
INNER JOIN status
ON ngstest.statusid = status.statusid)
INNER JOIN dbo_patient_table
ON patients.patientid = dbo_patient_table.patienttrustid)
LEFT JOIN dna
ON ngstest.dna = dna.dnanumber)
INNER JOIN status AS status_1
ON patients.s_statusoverall = status_1.statusid)
LEFT JOIN gw_gendertable
ON dbo_patient_table.genderid = gw_gendertable.genderid)
LEFT JOIN ngswesbatch
ON ngstest.wesbatch = ngswesbatch.ngswesbatchid)
LEFT JOIN checker AS checker_1
ON ngstest.check1id = checker_1.check1id)
LEFT JOIN checker AS checker_2
ON ngstest.check2id = checker_2.check1id)
LEFT JOIN checker AS checker_3
ON ngstest.check3id = checker_3.check1id)
LEFT JOIN ngspanel AS ngspanel_2
ON ngstest.ngspanelid_c = ngspanel_2.ngspanelid)
LEFT JOIN checker AS checker_4
ON ngstest.check4id = checker_4.check1id
WHERE ((ngstest.referralid IN
(SELECT referralid FROM referral
WHERE grouptypeid = 14)
AND ngstest.ngstestid IN
(SELECT ngstest.ngstestid
FROM ngsanalysis
INNER JOIN ngstest
ON ngsanalysis.ngstestid = ngstest.ngstestid
WHERE ngsanalysis.pedigree = 3302) )
AND status.statusid = 1202218800)
ORDER BY ngstest.priority,
ngstest.daterequested;
The two nested queries are strings from elsewhere in the code so are called in the vba as " & includereferralls & " And " & ParentsStatusesFilter & "
They are:
ParentsStatusesFilter = "NGSTest.NGSTestID in
(SELECT NGSTest.NGSTestID
FROM NGSAnalysis
INNER JOIN NGSTest
ON NGSAnalysis.NGSTestID = NGSTest.NGSTestID
WHERE NGSAnalysis.Pedigree IN (3302,3303,3304)"
And
includereferrals = "NGSTest.ReferralID
(SELECT referralid FROM referral WHERE referral.grouptypeid = 14)"
The query needs to remain readable (and therefore editable) so can't use things like Distinct, Group By or contain any Unions. Have tried Exists instead of In for the nested queries but that stops it from actually filtering the results.
WHERE EXISTS (SELECT NGSTest.NGSTestID
FROM NGSAnalysis
INNER JOIN NGSTest
ON NGSAnalysis.NGSTestID = NGSTest.NGSTestID
WHERE NGSAnalysis.Pedigree IN (3302,3303,3304)
So the exist clause you have there isn't tied to the outer query which would run similar to just added 1 = 1 to the where clause. I took your where clause and converted it. It should look something like this...
WHERE EXISTS (
SELECT referralid
FROM referral
WHERE grouptypeid = 14 AND ngstest.referralid = referral.referralid)
AND EXISTS (
SELECT ngsanalysis.ngstestid
FROM ngsanalysis
WHERE ngsanalysis.pedigree IN (3302,3303,3304) AND ngstest.ngstestid = ngsanalysis.ngstestid
)
AND status.statusid = 1202218800
Adding exists will speed it up a bit, but the the bulk of the slowness is the left joins. Access does not handle the left joins as well as SQL Server does. Change all your joins to inner joins and you will see the query runs very fast. This is obviously not ideal since some relationships are optional. What I have done to get around this is add a default record that replaces a null relationship.
Here is what that looks like for you: In the checker table you could add a record that represents a null value. So put a record into the checker table with check1id of -1 or 0. Then default check1id, check2id, check3id on ngstest to -1 or 0. You will need to do that type of thing for all tables you need to left join on.

LEFT JOIN in Query returning more records than desired

I'm working on an MS ACCESS query which should pull all available data from one main table (specified by FROM) and then joining three additional tables via LEFT JOIN. My issue is that the query is returning more records because of a one to many relationship.
Is there any way I can return only ONE record per line in the main table? Any help is greatly appreciated!! Thank you!
SELECT tbl_Item_PO.Item,
tbl_ItemSKUType.SKU,
tbl_Item_PO.[EX-FACTORY DATE],
tbl_Item_PO.[QTY PER SHIPMENT],
IIf(tbl_Item_PO.Item=Posted_Inv_Tran.Item And tbl_Item_PO.Whse=Posted_Inv_Tran.Whse And tbl_Item_PO.[ITR#]=Posted_Inv_Tran.[TRN Number],0,tbl_Item_PO.[QTY PER SHIPMENT]) AS QTY,
tbl_Item_PO.WHSE, tbl_WhseRegion.Region
FROM ((tbl_Item_PO
LEFT JOIN tbl_ItemSKUType ON tbl_Item_PO.item = tbl_ItemSKUType.Item)
LEFT JOIN tbl_WhseRegion ON tbl_Item_PO.WHSE = tbl_WhseRegion.Whse)
LEFT JOIN Posted_Inv_Tran ON (tbl_Item_PO.[ITR#] = Posted_Inv_Tran.[TRN Number]) AND (tbl_Item_PO.Whse = Posted_Inv_Tran.Whse) AND (tbl_Item_PO.Item = Posted_Inv_Tran.Item);
I've also tried creating sub-queries within the join statements that return only ONE record but I cannot get that to work either:
SELECT po.Item,
ist.SKU,
po.[EX-FACTORY DATE],
po.[QTY PER SHIPMENT],
IIf(po.Item=itr.Item And po.Whse=itr.Whse And po.[ITR#]=itr.[TRN Number],0,po.[QTY PER SHIPMENT]) AS QTY,
po.WHSE,
wh.Region
FROM ((tbl_Item_PO po
LEFT JOIN (select top 1 * from tbl_ItemSKUType
RIGHT JOIN tbl_Item_PO on tbl_ItemSKUType.Item = tbl_Item_PO.item
WHERE tbl_Item_PO.Item = tbl_ItemSKUType.Item)
ist ON po.item = ist.Item)
LEFT JOIN (select top 1 * from tbl_WhseRegion
RIGHT JOIN tbl_Item_PO on tbl_WhseRegion.Whse = tbl_Item_PO.Whse
WHERE tbl_Item_PO.whse = tbl_WhseRegion.Whse)
wh ON po.Whse = wh.Whse)
LEFT JOIN (select top 1 * from Posted_Inv_Tran
RIGHT JOIN tbl_Item_PO on Posted_Inv_Tran.[TRN Number] = tbl_Item_PO.[ITR#] AND Posted_Inv_Tran.Whse = tbl_Item_PO.Whse AND Posted_Inv_Tran.Item = tbl_Item_PO.Item
WHERE (tbl_Item_PO.[ITR#] = Posted_Inv_Tran.[TRN Number]) AND (tbl_Item_PO.Whse = Posted_Inv_Tran.Whse) AND (tbl_Item_PO.Item = Posted_Inv_Tran.Item))
it ON (po.[ITR#] = it.[TRN Number]) AND (po.Whse = it.Whse) AND (po.Item = it.Item);
You can achieve that with a group by clause. Make sure to list all fields in that clause which identify a line, and that all other expressions/fields you have in the select clause are aggregated with some aggregation function, like Min().
NB: Your IIf condition does not need to check all those fields, just one is enough: when one matches, all will match:
SELECT tbl_Item_PO.Item,
Min(tbl_ItemSKUType.SKU) AS SKU,
tbl_Item_PO.[EX-FACTORY DATE],
tbl_Item_PO.[QTY PER SHIPMENT],
Min(IIf(tbl_Item_PO.Item=Posted_Inv_Tran.Item, 0,
tbl_Item_PO.[QTY PER SHIPMENT])) AS QTY,
tbl_Item_PO.WHSE,
Min(tbl_WhseRegion.Region) AS Region
FROM ((tbl_Item_PO
LEFT JOIN tbl_ItemSKUType
ON tbl_Item_PO.item = tbl_ItemSKUType.Item)
LEFT JOIN tbl_WhseRegion
ON tbl_Item_PO.WHSE = tbl_WhseRegion.Whse)
LEFT JOIN Posted_Inv_Tran
ON (tbl_Item_PO.[ITR#] = Posted_Inv_Tran.[TRN Number])
AND (tbl_Item_PO.Whse = Posted_Inv_Tran.Whse)
AND (tbl_Item_PO.Item = Posted_Inv_Tran.Item)
GROUP BY tbl_Item_PO.Item,
tbl_Item_PO.[EX-FACTORY DATE],
tbl_Item_PO.[QTY PER SHIPMENT],
tbl_Item_PO.WHSE;

Simplifying where clause when a column is mutual for the tables at from clause

I would like to learn if there is any more efficient way to write the query below:
SELECT *
FROM requests srp
INNER JOIN surgeons rpsur
ON rpsur.id = srp.surgeon_id
LEFT OUTER JOIN #usersurgeons usersurgeons
ON usersurgeons.surgeon_id = srp.surgeon_id
LEFT OUTER JOIN surgeons LOsurgeons
ON usersurgeons.surgeon_id = LOsurgeons.id
LEFT OUTER JOIN provsurgeons LOprovsurgeons
ON LOprovsurgeons.id = LOsurgeons.provsurgeon_id
INNER JOIN #selectedsurgeons up
ON up.surgeon_id = rpsur.id
LEFT OUTER JOIN provsurgeons ps
ON ps.id = rpsur.provsurgeon_id
WHERE rpsur.isprimary = 0
AND usersurgeons.isprimary = 0
AND LOsurgeons.isprimary = 0
AND LOprovsurgeons.isprimary = 0
AND up.isprimary = 0
AND ps.isprimary = 0
I am not happy with the where clause here, is there any more professional way to write this, rather than adding the clauses to the join lines (such as on xx.id = yy.id and xx.isPrimary=0)??
From this query alone there are not many things that can be said. You should consider adding some more context (how do you get data into those temporary tables and the structure of %surgeons tables):
1) Select * makes almost impossible to use any index and also provides a lot of columns (Requests.*, surgeons.*, Provsurgeons.* etc.) in your final result. Return only the columns that you need.
2) If isPrimary = 0 filtering is performed often in your queries (not just this one), you can consider creating a view that fetches data filtered by isPrimary = 0. E.g. vwSurgeons, vwProvsurgeons. Then, you can just JOIN directly to the view instead of the table.
3) [already mentioned in the comments] Any condition that excludes NULL values for the OUTER JOINed table will transform the OUTER into INNER.
Instead of joining all tables and having a where clause at the end, use a derived tables only with filtered records. This way your query performance will be better.
SELECT *
FROM requests srp
INNER JOIN surgeons rpsur
ON rpsur.id = srp.surgeon_id
LEFT OUTER JOIN
(
SELECT *
FROM #usersurgeons
WHERE isprimary = 0
)usersurgeons
ON usersurgeons.surgeon_id = srp.surgeon_id
LEFT OUTER JOIN
(
SELECT *
FROM surgeons
WHERE isprimary = 0
)LOsurgeons
ON usersurgeons.surgeon_id = LOsurgeons.id
LEFT OUTER JOIN
(
SELECT *
FROM provsurgeons
WHERE isprimary = 0
)LOprovsurgeons
ON LOprovsurgeons.id = LOsurgeons.provsurgeon_id
INNER JOIN
(
SELECT *
FROM #selectedsurgeons
WHERE isprimary = 0
)up
ON up.surgeon_id = rpsur.id
LEFT OUTER JOIN
(
SELECT *
FROM provsurgeons
WHERE isprimary = 0
) ps
ON ps.id = rpsur.provsurgeon_id
WHERE rpsur.isprimary = 0

Access left join not working as I am picturing it

I am trying to write a query that involves 3 tables and left joining two of them onto a main one.
SELECT UNIT_MAIN.UNIT_NO, DEPT_MAIN.LEV_2, Card.CardNumberLong AS [Some
Number], Card.Enabled, F_CARD.CARD_NO, F_CARD.END_DT
FROM (((UNIT_MAIN
INNER JOIN DEPT_MAIN ON UNIT_MAIN.USING_DEPT = DEPT_MAIN.DEPT_ID)
LEFT JOIN Card ON (UNIT_MAIN.UNIT_NO = Card.UnitCode AND Card.Enabled = True) )
LEFT JOIN F_CARD ON (UNIT_MAIN.UNIT_ID = F_CARD.ASSIGNED_ID AND (F_CARD.END_DT) Is Null ))
WHERE (((UNIT_MAIN.STATUS)="A") AND ((DEPT_MAIN.LEV_2)="AM") AND ((Card.Enabled)=True) )
OR (((UNIT_MAIN.STATUS)="D") AND ((DEPT_MAIN.LEV_2)="AM") AND ((Card.Enabled)=True) )
The issue I am having is when F_CARD table has rows where the F_CARD.END_DT is not null, causing the main table (unit table) not to show up even though it is a left join and the F_CARD table rows did not satisfy the join condition (or I am to believe).
I don't have any where clauses on the F_CARD table and they are only on the join condition.
edit
When I perform
LEFT JOIN MFIVE_F_CARD ON (MFIVE_UNIT_DEPT_COMP_MAIN.UNIT_ID = MFIVE_F_CARD.ASSIGNED_ID AND ((MFIVE_F_CARD.END_DT) Is Null)
The unit does not appear if the F_CARD table contained rows that had an END_DT, I was to believe that since the left join condition failed, the inner table (unit table) should appear regardless.
If I remove any F_CARD related values from the query, the missing units I am looking for appear. It is an inner join, left join, left join. When the second left join happens, I lose rows even when they should appear.
I narrowed down my joins and just did the inner with the left with F_CARD. Trying to see why it doesnt return rows where the join fails.
Tried to do the following, however im getting an unsupported join error...
SELECT
UNIT_MAIN.UNIT_NO
,DEPT_MAIN.LEV_2
,Card.CardNumberLong AS [SomeNumber]
,Card.Enabled
,F_CARD.CARD_NO
,F_CARD.END_DT FROM
(
(
(
UNIT_MAIN
INNER JOIN DEPT_MAIN
ON UNIT_MAIN.USING_DEPT = DEPT_MAIN.DEPT_ID
)
LEFT JOIN Card
ON (UNIT_MAIN.UNIT_NO = Card.UnitCode AND Card.Enabled = True)
)
LEFT JOIN F_CARD
ON (UNIT_MAIN.UNIT_ID = F_CARD.ASSIGNED_ID AND F_CARD.END_DT Is Null)
) WHERE
(UNIT_MAIN.STATUS = "A" OR UNIT_MAIN.STATUS = "D")
AND DEPT_MAIN.LEV_2 = "AM"
Thanks.
I have a feeling it's because you have clauses in your joins that are doing boolean checks rather than matching records between the tables, i.e.
Card.Enabled = True and (F_CARD.END_DT) IS NULL
Try changing the query to:
SELECT
UNIT_MAIN.UNIT_NO
,DEPT_MAIN.LEV_2
,Card.CardNumberLong AS [SomeNumber]
,Card.Enabled
,F_CARD.CARD_NO
,F_CARD.END_DT
FROM
(
(
(
UNIT_MAIN
INNER JOIN DEPT_MAIN
ON UNIT_MAIN.USING_DEPT = DEPT_MAIN.DEPT_ID
)
LEFT JOIN Card
ON UNIT_MAIN.UNIT_NO = Card.UnitCode
)
LEFT JOIN F_CARD
ON UNIT_MAIN.UNIT_ID = F_CARD.ASSIGNED_ID
)
WHERE
(UNIT_MAIN.STATUS = "A" OR UNIT_MAIN.STATUS = "D")
AND DEPT_MAIN.LEV_2 = "AM"
AND Card.Enabled = True

Performing left join along with where condition

I was using this query to get results,
SELECT *
FROM TRXN_REPORT CNT,
TBL_CUSTINFO CUSTADDINFO,
TBL_DEAL_EVT EVENT,
TBL_MISCADDR CIFMISC
WHERE CNT.INTERNAL_REF_NUM = EVENT.INTERNAL_REF_NUM
AND CNT.BRANCH = EVENT.BRANCH
AND CNT.QFXVERSION = EVENT.QFXVERSION
AND CNT.VALUEDATE = EVENT.VALUEDATE
AND CNT.LIQ_STATUS = 'L'
AND EVENT.EVENTCODE = 'LLIQ' -- Liquidation Event
AND CUSTADDINFO.CUSTOMER_NO = CNT.CUST_NUM
AND CIFMISC.CUSTOMER_NUMBER = CNT.CUST_NUM
AND CUSTADDINFO.REC_STATUS ='O'
AND CIFMISC.REC_STATUS = 'O'
AND MY_PBG_CUSTOMER = 'Y'
AND (BUYCCYCODE = 'USD' OR SELLCCYCODE ='USD')
here this condition
AND CIFMISC.CUSTOMER_NUMBER = CNT.CUST_NUM
Is fetching only those records which are present both in TBL_MISCADDR and TRXN_REPORT table, now as a change of requirment I want to perform left outer join so as to get records which are not there in TBL_MISCADDR table but present in TRXN_BATCHREPORT_WRK. How to perform this along with maintaining all the where conditions.
You have to you specify the condition for the join in the ON clause of the JOIN. So, your current conditions in the WHERE clause should be separated into the conditions for joining the tables (and these moved to ON clause), and the rest for selecting the lines - and these should be left in the WHERE clause. Example (don't know if I have it correct according to your data but you will get the idea):
SELECT *
FROM TRXN_REPORT CNT
LEFT JOIN TBL_DEAL_EVT EVENT ON
CNT.INTERNAL_REF_NUM = EVENT.INTERNAL_REF_NUM
AND CNT.BRANCH = EVENT.BRANCH
AND CNT.QFXVERSION = EVENT.QFXVERSION
AND CNT.VALUEDATE = EVENT.VALUEDATE
LEFT JOIN SOME_OTHER_TABLE T ON
...
WHERE
AND CUSTADDINFO.REC_STATUS ='O'
AND CIFMISC.REC_STATUS = 'O'
AND MY_PBG_CUSTOMER = 'Y'
AND (BUYCCYCODE = 'USD' OR SELLCCYCODE ='USD')
....
This way you can safely use LEFT JOIN and the lines for which the records doesn't exist in the other table will also be included.
... to get records which are not there in TBL_MISCADDR table but
present in TRXN_BATCHREPORT_WRK.
That would be done using TRXN_BATCHREPORT_WRK LEFT JOIN TBL_MISCADDR ON <condition>.