Hi,
First off I stress I have this working perfectly using 3 visual queries. I would really like to further understand SQL nested queries though and combine the three visual queries into one SQL query. (Hope that makes sense!)
Basically I can insert the sub-query into the query as an expression but I dont understand how to reference fields within the sub query from the parent query.
The purpose of the query is to calculate the length difference in aluminium blocks between two stock takes (dates). It therefore shows the usage of these
The first query (Query1) (shown below) looks up all the lengths of all the different aluminium blocks processed at stock take A:
The second query (Query2) (shown below) looks up the lengths of all the different blocks processed at stock take date B:
Finally, the third query pulls the information together. It finds the difference in length of all matching blocks. If a block is found in Query1 (most recent date) that doesnt exist in Query2 (oldest date) then it assumes the block was processed somewhere in between the two stock takes and therefore Takes the value in Query 1 away from the original length of the block. If a block exists in Query2 but not in Query1 then a value of 0 is inserted (as the block has been used up completely). The information is then grouped using a concatenated string representing the type of aluminium each block is made from and each block that belongs to that type is added together. This is shown below
Sorry this is ever so slightly long winded.
Thanks a lot
Dave
First off I stress I have this working perfectly using 3 visual queries. I would really like to further understand SQL nested queries though and combine the three visual queries into one SQL query. (Hope that makes sense!)
Basically I can insert the sub-query into the query as an expression but I dont understand how to reference fields within the sub query from the parent query.
The purpose of the query is to calculate the length difference in aluminium blocks between two stock takes (dates). It therefore shows the usage of these
The first query (Query1) (shown below) looks up all the lengths of all the different aluminium blocks processed at stock take A:
Code:
SELECT TblHoneycombBlock.BlockID, TblStockTake.StockTakeLength, TblStockTake.StockTakeDateID, TblHoneycombBlock.OriginalLength
FROM TblHoneycombBlock RIGHT JOIN TblStockTake ON TblHoneycombBlock.BlockID = TblStockTake.BlockNoID
GROUP BY TblHoneycombBlock.BlockID, TblStockTake.StockTakeLength, TblStockTake.StockTakeDateID, TblHoneycombBlock.OriginalLength
HAVING (((TblStockTake.StockTakeDateID) Like [Forms]![FrmReports]![CboUsageStockTakeDate1]));
The second query (Query2) (shown below) looks up the lengths of all the different blocks processed at stock take date B:
Code:
SELECT TblHoneycombBlock.BlockID, TblStockTake.StockTakeLength, TblStockTake.StockTakeDateID, TblHoneycombBlock.OriginalLength
FROM TblHoneycombBlock RIGHT JOIN TblStockTake ON TblHoneycombBlock.BlockID = TblStockTake.BlockNoID
GROUP BY TblHoneycombBlock.BlockID, TblStockTake.StockTakeLength, TblStockTake.StockTakeDateID, TblHoneycombBlock.OriginalLength
HAVING (((TblStockTake.StockTakeDateID) Like [Forms]![FrmReports]![CboUsageStockTakeDate2]));
Finally, the third query pulls the information together. It finds the difference in length of all matching blocks. If a block is found in Query1 (most recent date) that doesnt exist in Query2 (oldest date) then it assumes the block was processed somewhere in between the two stock takes and therefore Takes the value in Query 1 away from the original length of the block. If a block exists in Query2 but not in Query1 then a value of 0 is inserted (as the block has been used up completely). The information is then grouped using a concatenated string representing the type of aluminium each block is made from and each block that belongs to that type is added together. This is shown below
Code:
SELECT [Density] & "-" & [CellSizeNumerator] & "/" & [CellSizeDenominator] & " - " & [FoilThickness] & [CoreType] & "-" & [Grade] AS Type, Sum(IIf([Query2]![BlockID] Not In ([Query1]![BlockID]),"0",IIf([Query2]![BlockID] In ([Query1]![BlockID]),[Query2]![StockTakeLength]-[Query1]![StockTakeLength],[Query1]![OriginalLength]-[Query1]![StockTakeLength]))) AS [Usage]
FROM TblGrade RIGHT JOIN (TblCoreType RIGHT JOIN ((TblHoneycombBlock LEFT JOIN Query2 ON TblHoneycombBlock.BlockID = Query2.BlockID) LEFT JOIN Query1 ON TblHoneycombBlock.BlockID = Query1.BlockID) ON TblCoreType.ID = TblHoneycombBlock.NormalPerforated) ON TblGrade.ID = TblHoneycombBlock.GradeID
GROUP BY TblGrade.Grade, TblHoneycombBlock.Density, [Density] & "-" & [CellSizeNumerator] & "/" & [CellSizeDenominator] & " - " & [FoilThickness] & [CoreType] & "-" & [Grade]
HAVING (((Sum(IIf([Query2]![BlockID] Not In ([Query1]![BlockID]),"0",IIf([Query2]![BlockID] In ([Query1]![BlockID]),[Query2]![StockTakeLength]-[Query1]![StockTakeLength],[Query1]![OriginalLength]-[Query1]![StockTakeLength])))) Is Not Null))
ORDER BY TblGrade.Grade, TblHoneycombBlock.Density;
Sorry this is ever so slightly long winded.
Thanks a lot
Dave