显示来自SQL的过滤信息

时间:2014-01-22 23:33:56

标签: sql having pymssql

我正在尝试编写一个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

1 个答案:

答案 0 :(得分: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

有没有更好的解决方案?由于我是新手,我很感激反馈。