我有一个不返回我正在寻找的结果的子查询。在我的情况下,我有 3 个位置并不总是具有日期范围的订单计数,并且它们没有返回结果为 0。相反,我丢失了这些部分的所有其他数据。我确信有更好的方法来查询数据,所以这是我的查询。
SELECT b.Part, b.Location, b.Last_12_Mo, b.Last_6_Mo,
COUNT(sd.SBINV) AS Order_Count
FROM (
SELECT a.Part, a.Location, a.Last_12_Mo,
ISNULL(SUM(sd.SBQSHP), 0) AS Last_6_Mo
FROM (
SELECT t3.Part, sd.SBLOC AS Location, ISNULL(SUM(sd.SBQSHP),0) AS Last_12_Mo
FROM Top300 t3
LEFT JOIN SalesData sd
ON t3.Part = sd.SBITEM
AND sd.SBINDT > '20120315'
GROUP BY t3.Part, sd.SBLOC
) AS a
LEFT JOIN SalesData sd
ON a.Part = sd.SBITEM
AND sd.SBINDT > '20121015'
GROUP BY a.Part, a.Location, a.Last_12_Mo
) AS b
LEFT JOIN SalesData sd
ON b.Part = sd.SBITEM
AND sd.SBINDT > '20130110'
GROUP BY b.Part, b.Location, b.Last_12_Mo, b.Last_6_Mo
ORDER BY b.Part, b.Location