对于我的硕士论文,我正在尝试检索订单ID的列表或CSV,项目名称,已发货的数量以及此特定订单的宗地数量。
我目前的问题是忽略with语句中的限制。 with语句的想法是排除包含可能在以后的回归中导致问题的不需要的文章的所有订单。重要的是,要么排除包含集合的所有订单,要么从列表中排除所有设置项目,但保留各个位置。应始终排除某些客户的订单或带有虚拟物品的订单。
我当前的查询如下所示:
USE XYZ;
DECLARE @@ORDERS AS INTEGER = 100;
-- Create a list of orders that fulfill all requirements
WITH INCLUDE AS (
SELECT DISTINCT OINV.DocEntry
FROM OINV
INNER JOIN INV1 ON OINV.DocEntry = INV1.DocEntry
INNER JOIN OITM ON INV1.ItemCode = OITM.ItemCode
WHERE
-- Exclude Sets
OITM.TreeType = 'N'
-- Excludes 'special' customers
AND OINV.CardCode NOT IN ('111', '222')
-- Exclude Dummy Articles
AND OITM.ItemCode NOT IN ('A', 'B')
)
SELECT OINV.DocNum AS 'ID', OITM.ItemName AS 'xName',
CAST(INV1.PackQty AS INT) AS 'xQuantity', AVG(T.Parcels) AS 'yQuantity'
FROM INV1
-- For a constant item name
INNER JOIN OITM ON INV1.ItemCode = OITM.ItemCode
-- For the order number (may be of some interest later)
INNER JOIN OINV ON OINV.DocEntry = INV1.DocEntry
-- Limit query to latest n orders (incl. parcels shipped for one order)
INNER JOIN (
SELECT TOP (@@ORDERS) OINV.DocEntry,
AVG(LEN(REPLACE(REPLACE(CAST(OINV.Tracing AS NVARCHAR(MAX)), ',',''),'/','')) / 14) AS 'Parcels'
FROM OINV
-- Only include 'accepted' orders
INNER JOIN INCLUDE ON INCLUDE.DocEntry = OINV.DocEntry
-- Only if the order has a tracing number and thus a count
WHERE OINV.Tracing IS NOT NULL
GROUP BY OINV.DocDate, OINV.DocEntry
HAVING AVG(LEN(REPLACE(REPLACE(CAST(OINV.U_DUR_Tracing AS NVARCHAR(MAX)
), ',',''),'/','')) / 14) > 0
ORDER BY OINV.DocDate DESC
) T ON INV1.DocEntry = T.DocEntry
GROUP BY OINV.DocNum, OITM.ItemName, OITM.ItemCode, INV1.PackQty
ORDER BY ID
我非常感谢任何建议。