如何从EmniyetStok获取所有数据。例如,EmniyetStok有1000行,并加工其他表200行。下面的查询返回给我200行。但我想采取所有emniyetstok(所有1000行)。我使用左连接(对于EmniyetStok),但它不起作用。我想带走所有的Emniyetstok(主表)。如果t.Quantity为null或为空,则在查询exeqution结束时,它应为零。
请不要删除:我需要的{0} clouse。在哪里,它应该返回1000行。 “WHERE e.CustomerID ='0061004287'AND t.SapContractNumber ='3120012161'”我将添加。
SELECT
s.MaterialNumber,
sum(s.Stock) Stock,
dbo.getSapContractNumberBySapDeliveryNumber(s.SapDeliveryNumber) SapContractNumber,
s.BillTo,
s.Quantity
INTO #tmp
FROM Stok s
GROUP BY s.MaterialNumber, s.SapDeliveryNumber, s.BillTo, s.Quantity
-----------------------
-----------------------
SELECT
e.SapContractNumber [Anlaşma No],
c.ContractName [Anlaşma Adı],
e.CustomerId [Müşteri No],
cu.CustomerFullName [Müşteri Adı],
e.MaterialNumber [Ürün No],
sc.MaterialName [Ürün],
convert(decimal(10,2),avg(EmniyetStok)) [Emniyet Stok],
convert(decimal(10,2),avg(OrtalamaStok)) [OrtalamaStok],
convert(decimal(10,2),sum(Stock)) Stok,
convert(decimal(10,2),avg(e.ortalamastok) - sum(t.Quantity)) SiparisOnerisi
FROM EmniyetStok e
LEFT JOIN #tmp t
ON t.MaterialNumber = e.MaterialNumber
AND t.SapContractNumber = e.SapContractNumber
AND e.CustomerID = t.BillTo
LEFT JOIN Contracts c ON c.SAPContractNumber = e.SapContractNumber
LEFT JOIN Customers cu ON cu.CustomerID = e.CustomerID
LEFT JOIN StockCards sc ON sc.MaterialNumber = e.MaterialNumber
WHERE {0}
GROUP BY
e.SapContractNumber,
c.ContractName,
e.CustomerId,
cu.CustomerFullName,
e.MaterialNumber,
sc.MaterialName
HAVING sum(t.Quantity) < avg(e.ortalamastok)
DROP TABLE #tmp
答案 0 :(得分:1)
您的问题出在字符串中:
HAVING sum(t.Quantity) < avg(e.ortalamastok)
当t.Quantity为null或e.ortalamastok为null时,您将丢失记录。 С错误并检查:
具有总和(ISNULL(t.Quantity,0))&lt; AVG(ISNULL(e.ortalamastok,0))