Thanks for the help. Here is the logic. I need a View that will return the total available inventory. This inventory is the logical difference from tblRDLine and tblRSTLine. In addition there is a lookup to determine a multiplier that normalizes the two numbers. The parts look basically like:
tblRDLine - the quantity origin in inventory
A lookup multiplier to put "quantity" in the same metric as the next table (which if it matters is pounds)
tblRSTLine - inventory that is gone
There is a common KEY between tblRSTLine and tblRDLine of RDLineID.
The formula should be "(SUM(quantity) * lookup) - SUM(rstQty))" is what's left available for each RDLineID.
I'm close with the below but I believe it requires a subquery which is beyond what I know. Here is my larger code:
Help is greatly appreciated. If anyone is wondering all these CASTS are to overcome some issues with queries and EF4. I understand that in pure SQL they aren't required.
tblRDLine - the quantity origin in inventory
Code:
dbo.tblRDLine.quantity
A lookup multiplier to put "quantity" in the same metric as the next table (which if it matters is pounds)
Code:
ISNULL(CAST(dbo.tblRDLine.rdActLbs / dbo.tblRDLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblTOLine.toActLbs / dbo.tblTOLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblPOLine.poActLbs / dbo.tblPOLine.unitQty AS decimal(9, 2)), dbo.tblRawPack.convPounds)))
tblRSTLine - inventory that is gone
Code:
ISNULL(dbo.tblRSTLine.rstQty, 0)
There is a common KEY between tblRSTLine and tblRDLine of RDLineID.
The formula should be "(SUM(quantity) * lookup) - SUM(rstQty))" is what's left available for each RDLineID.
I'm close with the below but I believe it requires a subquery which is beyond what I know. Here is my larger code:
Code:
SELECT dbo.tblRDLine.quantity * ISNULL(CAST(dbo.tblRDLine.rdActLbs / dbo.tblRDLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblTOLine.toActLbs / dbo.tblTOLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblPOLine.poActLbs / dbo.tblPOLine.unitQty AS decimal(9, 2)), dbo.tblRawPack.convPounds))) - ISNULL(dbo.tblRSTLine.rstQty, 0) AS quantity, dbo.tblPOLine.transDesc, dbo.tblRawPack.packDesc, dbo.tblRawPack.convPounds, CAST(dbo.tblPOLine.poActLbs / dbo.tblPOLine.unitQty AS decimal(9, 4)) AS poActLbs, CAST(dbo.tblTOLine.toActLbs / dbo.tblTOLine.quantity AS decimal(9, 4)) AS toActLbs, CAST(dbo.tblRDLine.rdActLbs / dbo.tblRDLine.quantity AS decimal(9, 4)) AS rdActLbs, dbo.tblRDLine.RDLineID, dbo.tblRDLine.quantity AS Expr1, ISNULL(CAST(dbo.tblRDLine.rdActLbs / dbo.tblRDLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblTOLine.toActLbs / dbo.tblTOLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblPOLine.poActLbs / dbo.tblPOLine.unitQty AS decimal(9, 2)), dbo.tblRawPack.convPounds))) AS Expr2, ISNULL(dbo.tblRSTLine.rstQty, 0)AS Expr3
FROM dbo.tblRDLine INNER JOIN
dbo.tblTOLine ON dbo.tblRDLine.TOLineID = dbo.tblTOLine.TOLineID INNER JOIN dbo.tblPOLine ON dbo.tblTOLine.POLineID = dbo.tblPOLine.POLineID INNER JOIN dbo.tblRawPack ON dbo.tblPOLine.rawPackID = dbo.tblRawPack.rawPackID INNER JOIN dbo.tblVarietal ON dbo.tblPOLine.varietalID = dbo.tblVarietal.varietalID INNER JOIN dbo.tblOrigin ON dbo.tblVarietal.originID = dbo.tblOrigin.originID INNER JOIN dbo.tblGrade ON dbo.tblPOLine.gradeID = dbo.tblGrade.gradeID INNER JOIN dbo.tblGradeDensity ON dbo.tblPOLine.DensityID = dbo.tblGradeDensity.DensityID INNER JOIN dbo.tblGradeLength ON dbo.tblPOLine.LengthID = dbo.tblGradeLength.LengthID INNER JOIN dbo.tblGradeScreen ON dbo.tblPOLine.ScreenID = dbo.tblGradeScreen.ScreenID LEFT OUTER JOIN dbo.tblRSTLine ON dbo.tblRDLine.RDLineID = dbo.tblRSTLine.RDLineID
GROUP BY dbo.tblPOLine.transDesc, dbo.tblRawPack.packDesc, dbo.tblRawPack.convPounds, CAST(dbo.tblPOLine.poActLbs / dbo.tblPOLine.unitQty AS decimal(9, 4)), CAST(dbo.tblTOLine.toActLbs / dbo.tblTOLine.quantity AS decimal(9, 4)), CAST(dbo.tblRDLine.rdActLbs / dbo.tblRDLine.quantity AS decimal(9, 4)), dbo.tblRDLine.RDLineID, dbo.tblRDLine.quantity, ISNULL(CAST(dbo.tblRDLine.rdActLbs / dbo.tblRDLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblTOLine.toActLbs / dbo.tblTOLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblPOLine.poActLbs / dbo.tblPOLine.unitQty AS decimal(9, 2)), dbo.tblRawPack.convPounds))), ISNULL(dbo.tblRSTLine.rstQty, 0), dbo.tblRDLine.quantity * ISNULL(CAST(dbo.tblRDLine.rdActLbs / dbo.tblRDLine.quantity AS decimal(9,2)), ISNULL(CAST(dbo.tblTOLine.toActLbs / dbo.tblTOLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblPOLine.poActLbs / dbo.tblPOLine.unitQty AS decimal(9, 2)), dbo.tblRawPack.convPounds))) - ISNULL(dbo.tblRSTLine.rstQty, 0)
HAVING (dbo.tblRDLine.quantity * ISNULL(CAST(dbo.tblRDLine.rdActLbs / dbo.tblRDLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblTOLine.toActLbs / dbo.tblTOLine.quantity AS decimal(9, 2)), ISNULL(CAST(dbo.tblPOLine.poActLbs / dbo.tblPOLine.unitQty AS decimal(9, 2)), dbo.tblRawPack.convPounds))) - ISNULL(dbo.tblRSTLine.rstQty, 0) > 0)
Help is greatly appreciated. If anyone is wondering all these CASTS are to overcome some issues with queries and EF4. I understand that in pure SQL they aren't required.