0

我正在尝试编写一个 sql 语句来显示我们所有自己发货的包裹,以便我们可以更新我们的权重。只要我的信息都在一个列中,它就很好用。当我尝试从第二列添加 SKU 时,如果不对 sku 列进行分组,它将无法工作。

--Need to add [Order Details].SKU here while retaining only for orders that match having statement.
SELECT        TOP (50) Tracking.OrderNum, Tracking.Pounds, Tracking.Ounces
FROM            Tracking INNER JOIN
              [Order Details] ON Tracking.OrderNum = [Order Details].OrderNumber AND Tracking.OrderNum = [Order Details].OrderNumber 
--Adjustment differentiates items from charges like tax ect.
WHERE        ([Order Details].Adjustment = 0)
GROUP BY Tracking.OrderNum, Tracking.Pounds, Tracking.Ounces
HAVING        (COUNT([Order Details].OrderNumber) = 1)
ORDER BY CAST(Tracking.OrderNum AS INT) DESC
4

1 回答 1

0

我最终使用了这个:

WITH SingleShips AS
(
SELECT        Tracking.OrderNum, Tracking.Pounds, Tracking.Ounces
FROM            Tracking INNER JOIN
                         [Order Details] ON Tracking.OrderNum = [Order Details].OrderNumber
WHERE        ([Order Details].DetailDate > GETDATE()-180) AND [Order Details].Adjustment=0 
             --and Tracking.OrderNum = 216986

GROUP BY Tracking.OrderNum, Tracking.Pounds, Tracking.Ounces
HAVING        (COUNT([Order Details].OrderNumber) = 1) AND SUM([Order Details].QuantityOrdered) = 1
)

SELECT        [Order Details].DetailDate, [Order Details].SKU, SingleShips.*
FROM            SingleShips INNER JOIN
                        [Order Details] ON SingleShips.OrderNum = [Order Details].OrderNumber 
WHERE        ([Order Details].Adjustment = 0) and SingleShips.OrderNum = [Order Details].OrderNumber
ORDER BY SKU

有人有更好的解决方案吗?因为我是新手,所以我很感激反馈。

于 2014-01-23T17:01:31.630 回答