我最近有一个善良的灵魂协助我创造一个我无法绕过头脑的计算...现在我面临一个额外的问题,如果它是多余的道歉。以下查询适用于WHERE
子句中列出的一篇文章。当我尝试从初始MAX
中删除SELECT
并将WHERE
更改为IN
然后添加多个选项时,计算失败。以下是每个方案的示例结果查询。
谢谢!
查询:
SELECT MAX(MATERIAL) AS [MATERIAL],
SUM(COUNT_ATP_QTY)/ CONVERT(FLOAT,COUNT(1)) AS [FULL_SIZE_ATP]
FROM
(
SELECT A.MATERIAL,
M.SIZE_LITERAL,
DATEPART(mm,AVAIL_DATE) AS [MONTH],
ISNULL(a.avail_qty,'0') AS [Avail_Qty],
COUNT(AVAIL_QTY) AS [COUNT_ATP_QTY]
FROM VW_MM_SALES_GRID M
LEFT OUTER JOIN VW_ATP_DOWNLOAD_NOREQCAT A
ON A.MATERIAL = M.MATERIAL
AND A.SIZE_LITERAL = M.SIZE_LITERAL
AND A.STOCK_CATEGORY IN ('A60381000','A60382000')
AND A.AVAIL_DATE BETWEEN GETDATE() AND GETDATE() + 90
WHERE M.MATERIAL ='AA2721' AND M.ACTIVE_FLAG IS NOT NULL
GROUP BY A.MATERIAL,
M.SIZE_LITERAL,
A.AVAIL_DATE,
A.AVAIL_QTY,
A.STOCK_CATEGORY
) A
一个材料的结果:
MATERIAL FULL_SIZE_ATP
------------------ ----------------------
AA2721 0.666666666666667
如果我添加另一篇文章,AA2720:
MATERIAL FULL_SIZE_ATP
------------------ ----------------------
NULL 0
AA2721 1
AA2720应为0,但AA2721的计算更改,并且AA2720的材料编号也没有填充
谢谢!
答案 0 :(得分:0)
我不认为将内部选择语句的等于更改为输入有任何问题,但是如果你删除了“最大”数据,那么在外部查询中,您需要告诉它如何对结果行进行分组。内部查询在其上有一个分组,但外部查询不在您提供的内容中。
SELECT MATERIAL AS [MATERIAL],
SUM(COUNT_ATP_QTY)/ CONVERT(FLOAT,COUNT(1)) AS [FULL_SIZE_ATP]
FROM
(
SELECT A.MATERIAL,
M.SIZE_LITERAL,
DATEPART(mm,AVAIL_DATE) AS [MONTH],
ISNULL(a.avail_qty,'0') AS [Avail_Qty],
COUNT(AVAIL_QTY) AS [COUNT_ATP_QTY]
FROM VW_MM_SALES_GRID M
LEFT OUTER JOIN VW_ATP_DOWNLOAD_NOREQCAT A
ON A.MATERIAL = M.MATERIAL
AND A.SIZE_LITERAL = M.SIZE_LITERAL
AND A.STOCK_CATEGORY IN ('A60381000','A60382000')
AND A.AVAIL_DATE BETWEEN GETDATE() AND GETDATE() + 90
WHERE M.MATERIAL in ('AA2721', 'OtherValue') AND M.ACTIVE_FLAG IS NOT NULL
GROUP BY A.MATERIAL,
M.SIZE_LITERAL,
A.AVAIL_DATE,
A.AVAIL_QTY,
A.STOCK_CATEGORY
) A
GROUP BY MATERIAL