我正在尝试在SQL Server Management Studio 2012中运行以下代码:
SELECT
ib.IFLOC AS Location,
ib.IFITEM AS KitPart,
im.ICDSC1 AS KitPart_Description,
kd.ILCMPN AS ComponentPart,
ib.IFPRVN AS Vendor,
ib.IFSTK AS Stock,
ib.IFLRD AS Last_Receipt_Date,
ib.IFLEAD AS Lead_Time,
vn.ACBUYR AS Buyer,
iv.IGMORD AS MOQ,
SUM(ib.IFQOH) AS QOH,
SUM(ib.IFQPO) AS QOO,
SUM(ib.IFQCM) AS QCM,
SUM(ib.IFQBO) AS QBO,
CAST(ISNULL(SUM(CASE WHEN sd.SBINDT > DateAdd(day, - 30, GetDate()) THEN sd.SBQSHP ELSE 0 END), 0) AS DECIMAL(8, 2)) AS Last_30_Days,
CAST(ISNULL(SUM(CASE WHEN sd.SBINDT BETWEEN DateAdd(day, - 60, GetDate()) AND DateAdd(day, - 31, GetDate()) THEN sd.SBQSHP ELSE 0 END), 0) AS DECIMAL(8, 2)) AS Thirty1_to_60_Days,
CAST(ISNULL(SUM(CASE WHEN sd.SBINDT BETWEEN DateAdd(day, - 90, GetDate()) AND DateAdd(day, - 61, GetDate()) THEN sd.SBQSHP ELSE 0 END), 0) AS DECIMAL(8, 2)) AS Sixty1_to_90_Days,
CAST(ISNULL(SUM(CASE WHEN sd.SBINDT > DateAdd(day, - 90, GetDate()) THEN sd.SBQSHP ELSE 0 END), 0) AS DECIMAL(6, 2)) AS L90,
CAST(ISNULL(SUM(CASE WHEN sd.SBINDT BETWEEN DateAdd(day, - 120, GetDate()) AND DateAdd(day, - 91, GetDate()) THEN sd.SBQSHP ELSE 0 END), 0) AS DECIMAL(8, 2)) AS Ninety1_to_120_Days,
CAST(ISNULL(SUM(CASE WHEN sd.SBINDT > DateAdd(day, - 365, GetDate()) THEN sd.SBQSHP ELSE 0 END), 0) AS DECIMAL(8, 2)) AS Last_12_Months,
SUM(ib.IFQOH - ib.IFQCM) AS Available,
CAST(ROUND(SUM(CAST(ISNULL(SUM(CASE WHEN sd.SBINDT > DateAdd(day, - 90, GetDate()) THEN sd.SBQSHP ELSE 0 END), 0) AS DECIMAL(6, 2)) / 12), 2) AS NUMERIC(8, 2)) AS 'Three_Mo_wkly_Avg',
CAST(ROUND(SUM(CAST(ISNULL(SUM(CASE WHEN sd.SBINDT > DateAdd(day, - 365, GetDate()) THEN sd.SBQSHP ELSE 0 END), 0) AS DECIMAL(8, 2)) / 52), 2) AS NUMERIC(8, 2)) AS 'L12Mo_wkly_Avg',
CASE WHEN tm.Part IS NOT NULL THEN 'Y' ELSE 'N' END AS TopMover,
CASE WHEN irl.Part IS NOT NULL THEN 'Y' ELSE 'N' END AS IRL,
CASE WHEN csl.Part IS NOT NULL THEN 'Y' ELSE 'N' END AS CSL,
CASE WHEN sgc.Part IS NOT NULL THEN 'Y' ELSE 'N' END AS SGC,
SUM(CASE WHEN po.PBDUE < CAST(GETDATE() AS DATE) THEN po.PBDQTY ELSE 0 END) AS PastDue,
SUM(CASE WHEN po.PACONF LIKE '%CONF%' AND po.PBDUE >= CAST(GETDATE() AS DATE) AND po.PBDUE <> '2049-12-31' THEN po.PBDQTY ELSE 0 END) AS Confirmed,
SUM(CASE WHEN po.PACONF NOT LIKE '%CONF%' AND po.PBDUE >= CAST(GETDATE() AS DATE) THEN po.PBDQTY WHEN po.PBDUE = '2049-12-31' THEN po.PBDQTY ELSE 0 END) AS Unconfirmed
FROM
S2K_IB AS ib
LEFT OUTER JOIN
S2K_IM AS im ON im.ICITEM = ib.IFITEM
LEFT OUTER JOIN
SalesData sd ON sd.SBITEM = ib.IFITEM AND sd.DefaultLocation = ib.IFLOC AND sd.SBTYPE = 'O'
LEFT OUTER JOIN
TopMovers tm ON tm.Part = ib.IFITEM
LEFT OUTER JOIN
IRL irl ON irl.Part = ib.IFITEM
LEFT OUTER JOIN
SpringGapCloser sgc ON sgc.Part = ib.IFITEM
LEFT OUTER JOIN
NewCSL csl ON csl.Part = ib.IFITEM
LEFT OUTER JOIN
PurchaseOrders po ON po.PBITEM = ib.IFITEM AND po.PBLOC = ib.IFLOC AND po.PBTYPE NOT IN ('D', 'R') AND po.PASTAT <> 'X' AND po.PBUSER <> 'MDALTON'
LEFT OUTER JOIN
S2K_IV iv ON iv.IGITEM = ib.IFITEM AND iv.IGLOC = ib.IFLOC AND iv.IGVND = ib.IFPRVN
LEFT OUTER JOIN
S2K_VEND vn ON vn.ACVEND = ib.IFPRVN
INNER JOIN
KitData kd ON kd.ILASSM = ib.IFITEM AND kd.ILLOC = ib.IFLOC
GROUP BY
ib.IFLOC,
ib.IFITEM,
im.ICDSC1,
kd.ILCMPN,
ib.IFPRVN,
ib.IFSTK,
ib.IFLRD,
ib.IFLEAD,
vn.ACBUYR,
iv.IGMORD,
tm.Part,
irl.Part,
csl.Part,
sgc.Part
ORDER BY
KitPart,
Location
我在这行代码中收到以下错误:
CAST(ROUND(SUM(CAST(ISNULL(SUM(CASE WHEN sd.SBINDT > DateAdd(day, - 90, GetDate()) THEN sd.SBQSHP ELSE 0 END), 0) AS DECIMAL(6, 2)) / 12), 2) AS NUMERIC(8, 2)) AS 'Three_Mo_wkly_Avg'
无法对包含聚合或子查询的表达式执行聚合函数。
如何重写此代码才能正常工作?