I have the following SQL which creates a view,
SELECT TOP (100) PERCENT X.StockCode AS topLevelPart, A.Component AS L1, B.Component AS L2, C.Component AS L3, D.Component AS L4, E.Component AS L5, F.Component AS L6, G.Component AS L7, H.Component AS L8
FROM (SELECT StockCode, PartCategory
FROM dbo.InvMaster
GROUP BY StockCode, PartCategory) AS X INNER JOIN
dbo.BomStructure AS A ON A.ParentPart = X.StockCode LEFT OUTER JOIN
dbo.BomStructure AS B ON B.ParentPart = A.Component LEFT OUTER JOIN
dbo.BomStructure AS C ON C.ParentPart = B.Component LEFT OUTER JOIN
dbo.BomStructure AS D ON D.ParentPart = C.Component LEFT OUTER JOIN
dbo.BomStructure AS E ON E.ParentPart = D.Component LEFT OUTER JOIN
dbo.BomStructure AS F ON F.ParentPart = E.Component LEFT OUTER JOIN
dbo.BomStructure AS G ON G.ParentPart = F.Component LEFT OUTER JOIN
dbo.BomStructure AS H ON H.ParentPart = G.Component
WHERE (B.StructureOffDate IS NULL) AND (C.StructureOffDate IS NULL) AND (D.StructureOffDate IS NULL) AND (E.StructureOffDate IS NULL) AND (A.StructureOffDate IS NULL) AND (F.StructureOffDate IS NULL) AND
(G.StructureOffDate IS NULL) AND (H.StructureOffDate IS NULL) OR
(B.StructureOffDate > GETDATE()) AND (C.StructureOffDate > GETDATE()) AND (D.StructureOffDate > GETDATE()) AND (E.StructureOffDate > GETDATE()) AND (A.StructureOffDate > GETDATE()) AND (F.StructureOffDate > GETDATE())
AND (G.StructureOffDate > GETDATE()) AND (H.StructureOffDate > GETDATE())
ORDER BY COALESCE (H.Component, G.Component, F.Component, E.Component, D.Component, C.Component, B.Component, A.Component), topLevelPart
I have been asked if its possible to add the average of the last 6 months purchases for each component. I can get the averages with this code
SELECT AVG(PorMasterDetail.MPrice) AS AVGPrice, InvMaster.StockCode
FROM PorMasterDetail INNER JOIN
PorMasterHdr ON PorMasterDetail.PurchaseOrder = PorMasterHdr.PurchaseOrder INNER JOIN
InvMaster ON PorMasterDetail.MStockCode = InvMaster.StockCode
WHERE (PorMasterHdr.OrderDueDate > DATEADD(m, - 6, GETDATE())) AND (PorMasterHdr.CancelledFlag <> 'Y')
GROUP BY InvMaster.StockCode
HAVING (SUM(PorMasterDetail.MOrderQty) > 0)
Is it possible to link the two ?
Thanks in advance
SELECT
X.StockCode AS topLevelPart
, avs.AVGprice
, A.Component AS L1
, B.Component AS L2
, C.Component AS L3
, D.Component AS L4
, E.Component AS L5
, F.Component AS L6
, G.Component AS L7
, H.Component AS L8
, COALESCE(H.Component, G.Component, F.Component, E.Component, D.Component, C.Component, B.Component, A.Component) AS sort_by
FROM (
SELECT
StockCode
, PartCategory
FROM dbo.InvMaster
GROUP BY StockCode
, PartCategory
) AS X
LEFT JOIN (
SELECT
InvMaster.StockCode
, AVG(PorMasterDetail.MPrice) AS AVGPrice
FROM PorMasterDetail
INNER JOIN PorMasterHdr ON PorMasterDetail.PurchaseOrder = PorMasterHdr.PurchaseOrder
INNER JOIN InvMaster ON PorMasterDetail.MStockCode = InvMaster.StockCode
WHERE (PorMasterHdr.OrderDueDate > DATEADD(m, -6, GETDATE()))
AND (PorMasterHdr.CancelledFlag <> 'Y')
GROUP BY InvMaster.StockCode
HAVING (SUM(PorMasterDetail.MOrderQty) > 0)
) avs ON X.StockCode = avs.StockCode
INNER JOIN dbo.BomStructure AS A ON A.ParentPart = X.StockCode
LEFT OUTER JOIN dbo.BomStructure AS B ON B.ParentPart = A.Component
LEFT OUTER JOIN dbo.BomStructure AS C ON C.ParentPart = B.Component
LEFT OUTER JOIN dbo.BomStructure AS D ON D.ParentPart = C.Component
LEFT OUTER JOIN dbo.BomStructure AS E ON E.ParentPart = D.Component
LEFT OUTER JOIN dbo.BomStructure AS F ON F.ParentPart = E.Component
LEFT OUTER JOIN dbo.BomStructure AS G ON G.ParentPart = F.Component
LEFT OUTER JOIN dbo.BomStructure AS H ON H.ParentPart = G.Component
WHERE (B.StructureOffDate IS NULL)
AND (C.StructureOffDate IS NULL)
AND (D.StructureOffDate IS NULL)
AND (E.StructureOffDate IS NULL)
AND (A.StructureOffDate IS NULL)
AND (F.StructureOffDate IS NULL)
AND (G.StructureOffDate IS NULL)
AND (H.StructureOffDate IS NULL)
OR (B.StructureOffDate > GETDATE())
AND (C.StructureOffDate > GETDATE())
AND (D.StructureOffDate > GETDATE())
AND (E.StructureOffDate > GETDATE())
AND (A.StructureOffDate > GETDATE())
AND (F.StructureOffDate > GETDATE())
AND (G.StructureOffDate > GETDATE())
AND (H.StructureOffDate > GETDATE())
;
dbo.BomStructure AS A ON A.ParentPart = X.StockCode LEFT OUTER JOIN
dbo.BomStructure AS B ON B.ParentPart = A.Component LEFT OUTER JOIN
SELECT AVG(PorMasterDetail.MPrice) AS AVGPrice, PorMasterDetail.MStockCode
FROM PorMasterDetail INNER JOIN
PorMasterHdr ON PorMasterDetail.PurchaseOrder = PorMasterHdr.PurchaseOrder
WHERE (PorMasterHdr.OrderDueDate > DATEADD(m, - 6, GETDATE())) AND (PorMasterHdr.CancelledFlag <> 'Y')
GROUP BY PorMasterDetail.MStockCode
HAVING (SUM(PorMasterDetail.MOrderQty) > 0)
Using data above would show
I can not really imagine, how the results look like, therefor an more generic answer.
You can create a view based on your fist view and add the calculated fields there (here AVG).
You can also create two independent views and mix them together in a third view, as long you have a unique ID in both source views so the view knows, what belongs together.
As the AVG view is a grouped view, you may have to construct the ID (i.e. by min, max,first, last aggregate function) as long as all records which are grouped together have the same ID.
The ID can be any field, what is common to the grouped AVG result, it just has to exist in both views.