MS Access - SQL LEFT JOIN multiple conditions - sql

I have this code which is working fine except that I need to add one more condition:
SELECT record1.*,
tbl_mpsregion.maintenanceteam,
tbl_mpsregion.regionmps
INTO tbl_sapforecast
FROM tbl_mpsregion
RIGHT JOIN
(
SELECT sap_ip19.*,
dateserial(RIGHT(trim([SAP_IP19].[PlanDate]),4),mid(trim([SAP_IP19].[PlanDate]),4,2),LEFT(trim([SAP_IP19].[PlanDate]),2)) AS [DATE/FORECAST],
tbl_labourstandard.re,
tbl_labourstandard.manning,
tbl_labourstandard.skillset AS skillset,
tbl_regionmapping.maintenanceplant,
tbl_regionmapping.area,
tbl_regionmapping.region AS region,
tbl_regionmapping.onresponse,
[RE]*[Manning]/60 AS hours
FROM (sap_ip19
LEFT JOIN tbl_labourstandard
ON (
LEFT(sap_ip19.[Task list description],3) = tbl_labourstandard.jemenawc)
AND (
sap_ip19.[MntPlan] = cdbl(tbl_labourstandard.supplypoint )))
LEFT JOIN tbl_regionmapping
ON sap_ip19.location = cdbl([Tbl_RegionMapping].[FittersDistricts])) AS record1
ON (
record1.region = [Tbl_MPSRegion].[Region])
AND (
record1.skillset = [Tbl_MPSRegion].[Skillset]) ;
Criteria to add is: If SAP_IP19.MntPlan does not match Tbl_LabourStandard.SupplyPoint then use 0 for Tbl_LabourStandard.SupplyPoint. I am not using Server 2000 so using CASE is not a solution. Have tried IIF and SWITCH but they are not taking query to sleep mode (not evaluating). I read that JOINS with IIF or SWITCH cannot be used. Please help!

You should be able to add if's or switches but you could always handle this with an OR - it's not the most performance friendly but it should get the job done, example below:
LEFT JOIN tbl_labourstandard
ON
(LEFT(sap_ip19.[Task list description],3) = tbl_labourstandard.jemenawc)
AND
((Tbl_LabourStandard.SupplyPoint = SAP_IP19.MntPlan AND
sap_ip19.[MntPlan] = cdbl(tbl_labourstandard.supplypoint))
OR (sap_ip19.[MntPlan] = 0))

Related

Issues with Joins where joins are reliant on 2 conditions - SQL Server 2014

Image - click here for sample data tables
I have attached an image of the tables and some sample data and I am trying to create a query that returns the following in the select statement, but am getting a bit confused and don't seem to be able to quite get the joins and where clauses right.
OFF_HIRES.Off_Hire_ID,
HIRE_CONTRACTS.Contract_Number,
MOBILE_JOB.MBW_Completed_Time,
Collection Charge - (This is the sum of the Charge_Amount for each HIRE_ITEMS.Contract_Number
where the HIRE_ITEMS.Stock_Number = 'COLLECTION' ),
TOTAL WEIGHT - (This is the sum of the STOCK_ITEMS.Weight * OFF_HIRE_ITEMS.QTY_OFF_HIRED)
The Links between the tables are as follows
OFF_HIRES.Contract_Number = Hire_Contracts.Contract_Number
OFF_HIRES.Off_hire_ID = OFF_HIRE_ITEMS.Off_hire_ID
HIRE_CONTRACTS.Contract_Number = HIRE_ITEMS.Contract_Number
HIRE_ITEMS.Stock_Number = STOCK_ITEMS.Stock_Number
MOBILE_JOB.Mbw_Doc_ID = OFF_HIRE_ITEMS.Off_Hire_Conntract_Number
and then there is a link between the OFF_HIRE_ITEMS and HIRE_ITEMS but requires both links to be true -
OFF_HIRE_ITEMS.Off_Hire_Contract_Number = HIRE_ITEMS.Contract_Number AND
OFF_HIRE_ITEMS.OH_ITEM_SEQ = HIRE_ITEMS.HIT_SEQ
You data structure is a little mess, but i think i got you:
SELECT
OFF_HIRES.Off_Hire_ID,
HIRE_CONTRACTS.Contract_Number,
(SELECT MOBILE_JOB.MBW_Completed_Time
FROM Mobile_JOB
WHERE Mobile_JOB.MB_Doc_ID = OFF_HIRES.Off_HIRE_ID) 'MBW_Completed_Time',
(SELECT SUM(Charge_Amount)
FROM HIRE_ITEMS
WHERE HIRE_ITEMS.Contract_Number = OFF_HIRES.Contract_Number
AND HIRE_ITEMS.Stock_Number = 'COLLECTION') 'SumCollectionCharge',
(SELECT SUM(ISNULL(STOCK_ITEMS.Weight,0) * OFF_HIRE_ITEMS.QTY_OFF_HIRED)
FROM HIRE_ITEMS
INNER JOIN OFF_HIRE_ITEMS
ON OFF_HIRE_TIEMS.OH_ITEM_SEQ = HIRE_ITEMS.HIT_SEQ
AND OFF_HIRE_ITEMS.Off_Hire_Contract_Number = HIRE_ITEMS.Contract_Number
INNER JOIN STOCK_ITEMS
ON STOCK_ITEMS.Stock_Number = HIRE_ITEMS.Stock_Number
) 'SumTotalWeight'
FROM OFF_Hires
INNER JOIN HIRE_CONTRACTS
ON HIRE_CONTRACTS.Contract_Number = OFF_Hires.Contract_Number

SQL Server - Need to SUM values in across multiple returned records

In the following query I am trying to get TotalQty to SUM across both the locations for item 6112040, but so far I have been unable to make this happen. I do need to keep both lines for 6112040 separate in order to capture the different location.
This query feeds into a Jasper ireport using something called Java.Groovy. Despite this, none of the PDFs printed yet have been either stylish or stained brown. Perhaps someone could address that issue as well, but this SUM issue takes priority
I know Gordon Linoff will get on in about an hour so maybe he can help.
DECLARE #receipt INT
SET #receipt = 20
SELECT
ent.WarehouseSku AS WarehouseSku,
ent.PalletId AS [ReceivedPallet],
ISNULL(inv.LocationName,'') AS [ActualLoc],
SUM(ISNULL(inv.Qty,0)) AS [LocationQty],
SUM(ISNULL(inv.Qty,0)) AS [TotalQty],
MAX(CAST(ent.ReceiptLineNumber AS INT)) AS [LineNumber],
MAX(ent.WarehouseLotReference) AS [WarehouseLot],
LEFT(SUM(ent.WeightExpected),7) AS [GrossWeight],
LEFT(SUM(inv.[Weight]),7) AS [NetWeight]
FROM WarehouseReceiptDetail AS det
INNER JOIN WarehouseReceiptDetailEntry AS ent
ON det.ReceiptNumber = ent.ReceiptNumber
AND det.FacilityName = ent.FacilityName
AND det.WarehouseName = ent.WarehouseName
AND det.ReceiptLineNumber = ent.ReceiptLineNumber
LEFT OUTER JOIN Inventory AS inv
ON inv.WarehouseName = det.WarehouseName
AND inv.FacilityName = det.FacilityName
AND inv.WarehouseSku = det.WarehouseSku
AND inv.CustomerLotReference = ent.CustomerLotReference
AND inv.LotReferenceOne = det.ReceiptNumber
AND ISNULL(ent.CaseId,'') = ISNULL(inv.CaseId,'')
WHERE
det.WarehouseName = $Warehouse
AND det.FacilityName = $Facility
AND det.ReceiptNumber = #receipt
GROUP BY
ent.PalletId
, ent.WarehouseSku
, inv.LocationName
, inv.Qty
, inv.LotReferenceOne
ORDER BY ent.WarehouseSku
The lines I need partially coalesced are 4 and 5 in the above return.
Create a second dataset with a subquery and join to that subquery - you can extrapolate from the following to apply to your situation:
First the Subquery:
SELECT
WarehouseSku,
SUM(Qty)
FROM
Inventory
GROUP BY
WarehouseSku
Now apply to your query - insert into the FROM clause:
...
LEFT JOIN (
SELECT
WarehouseSKU,
SUM(Qty)
FROM
Inventory
GROUP BY
WarehouseSKU
) AS TotalQty
ON Warehouse.WarehouseSku = TotalQty.WarehouseSku
Without seeing the actual schema DDL it is hard to know the exact cardinality, but I think this will point you in the right direction.

JOIN EXPRESSION NOT SUPPORTED with 3 tables

I'm working with MS Access, trying to join 3 tables together. But i'm getting message "JOIN EXPRESSION NOT SUPPORTED.".
Basically I want to join just 2 tables which are A_01HWeekEHCalendar and A00_Plant. A00_Plant requires 3 column to join; Plant_Product, Plant_Code, and Plant_Name. Plant_Product, Plant_Code is ok, but there is no column to match Plant_Product. so I have to add A_ProductGroup
I'm not sure if it can't be done in one Query or it's error from my SYNTAX. or there will be another way to do that without separate Queries.
I also add a picture of what I want and try to run and get error.
Thanks.
SELECT
A_01HWeekEHCalendar.RunNo_H,
A_01HWeekEHCalendar.Audit_Week,
A_01HWeekEHCalendar.CurrentYear,
A_01HWeekEHCalendar.CurrentWeek,
A_01HWeekEHCalendar.TTSMonth,
A_01HWeekEHCalendar.Audit_Plant,
A_01HWeekEHCalendar.Audit_plantname,
A_01HWeekEHCalendar.Audit_Qacode,
A_01HWeekEHCalendar.Audit_Qaname,
A_01HWeekEHCalendar.Audit_Product,
A00_Plant.HCA_StartDate
FROM
(A_01HWeekEHCalendar LEFT JOIN A_ProductGroup
ON A_01HWeekEHCalendar.Audit_Product = A_ProductGroup.R_Code)
LEFT JOIN A00_Plant
ON A_01HWeekEHCalendar.Audit_plantname = A00_Plant.Plant_Name
AND A_01HWeekEHCalendar.Audit_Plant = A00_Plant.Plant_Code
AND A_ProductGroup.R_Code = A00_Plant.Plant_Product;
You are lucky because you want only one column. You can work around this problem using a correlated subquery:
SELECT . . .,
(SELECT TOP (1) A00_Plant.HCA_StartDate
FROM A00_Plant
WHERE A_01HWeekEHCalendar.Audit_plantname = A00_Plant.Plant_Name
AND
A_01HWeekEHCalendar.Audit_Plant = A00_Plant.Plant_Code AND
A_ProductGroup.R_Code = A00_Plant.Plant_Product
) as HCA_StartDate
FROM A_01HWeekEHCalendar LEFT JOIN
A_ProductGroup
ON A_01HWeekEHCalendar.Audit_Product = A_ProductGroup.R_Code ;

Getting ambiguity error for a inventory SQL query where two fields should be equal to make the calculation. MS-ACCESS

Having this simple group of tables, I would like to make an inventory discriminating equal products that came from different providers, but I'm getting an "ambiguity error" running a query I though it would work. I don't know how to solve this.
Here's the query I tried:
SELECT tblProducts.product_Name,
tblProviders.provider,
Nz(Sum(tblIntakes.intake_QTY),0)-
Nz(Sum(tblExits.exit_QTY)) AS Stock
FROM tblProviders,
(tblProducts LEFT JOIN
tblExits
ON tblProducts.product_ID = tblExits.product_ID
) LEFT JOIN
tblIntakes
ON tblProducts.product_ID = tblIntakes.product_ID
GROUP BY tblProducts.product_Name, tblProviders.provider;
You may use subqueries in this case:
SELECT
tblProducts.product_Name,
tblProviders.provider,
Nz((
SELECT SUM(intake_QTY)
FROM tblIntakes
WHERE
tblIntakes.product_ID = tblProducts.product_ID AND
tblIntakes.provider_ID = tblProviders.provider_ID
), 0) -
Nz((
SELECT SUM(exit_QTY)
FROM tblExits
WHERE
tblExits.product_ID = tblProducts.product_ID AND
tblExits.provider_ID = tblProviders.provider_ID
), 0) AS Stock
FROM tblProviders, tblProducts;

SQL query for filtering data

I`m working on some sql queries to get some data out of a table; I have made 2 queries for the
same data but both give another result. The 2 queries are:
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN (
( patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id) AND
(patient.Sample = Samples.Sample)
) ON
(tissue.Sample_Name = data_overview.Sample_Name) AND
(tissue.Sample_Name = patient.Sample_Name)
WHERE data_overview.Sentrix_ID= 1416198
OR data_overview.Pool_ID='GS0005701-OPA'
OR data_overview.Pool_ID='GS0005702-OPA'
OR data_overview.Pool_ID='GS0005703-OPA'
OR data_overview.Pool_ID='GS0005704-OPA'
OR data_overview.Sentrix_ID= 1280307
ORDER BY Samples.Sample;")
And the other is
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN
(
(patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id)
AND (patient.Sample = Samples.Sample)) ON
(tissue.Sample_Name = data_overview.Sample_Name)
AND (tissue.Sample_Name = patient.Sample_Name)
WHERE ((
(data_overview.Sentrix_ID)=1280307)
AND (
(data_overview.Pool_ID)="GS0005701-OPA"
OR (data_overview.Pool_ID)="GS0005702-OPA"
OR (data_overview.Pool_ID)="GS0005703-OPA"
OR (data_overview.Pool_ID)="GS0005704-OPA"))
OR (((data_overview.Sentrix_ID)=1416198))
ORDER BY data_overview.Sample;
The one in the top is working quite well but it still won't filter the sentrix_ID.
The second 1 is created with Access but when I try to run this Query in R it gave
a unexpected symbol error. So if anyone knows how to create a query that filter POOL_ID and Sentrix_id with the given parameters thanks in advance
Is it a case of making the where clause something like this:
WHERE Sentrix_ID = 1280307 AND (Pool_ID = 'VAL1' OR Pool_ID = 'VAL2' OR Pool_ID = 'VAL3')
i.e. making sure you have brackets around the "OR" components?
Maybe you meant:
...
WHERE data_overview.Sentrix_ID IN (1280307,1416198 )
AND data_overview.Pool_ID IN ("GS0005701-OPA", "GS0005702-OPA", "GS0005703-OPA" ,"GS0005704-OPA")
;