查找超过特定SKU的订单

时间:2015-10-26 20:20:17

标签: mysql sql

我需要编写一个SQL查询,该查询将返回包含SKU ENROLL以上的所有订单号,但不应返回SKU ENROLL所在的订单号订单上唯一的SKU

在此示例中,此顺序将包含在查询结果中。 订单1001,包含SKU ENROLLSKU 688631

在此示例中,此顺序 NOT 将包含在查询结果中。 订单1003,包含SKU ENROLL

重要的是要注意,当返回查询结果时,它们看起来像这样

enter image description here

这是我到目前为止所写的内容,但我不确定其余内容。我收到了所有回复的人的反馈,并试图将其纳入但没有取得好成绩。

选择VO.DistID,VO.FirstName,VO.LastName,VO.OrderNumber,电子邮件,数量,Sku,OrderStatus FROM dbo.VwOrders AS VO INNER JOIN dbo.VwDistributor AS VD ON VO.DistID = VD.DistID INNER JOIN dbo.VwOrderLines AS VOL ON VO.OrderNumber = VOL.OrderNumber INNER JOIN dbo.VwInventory AS VI ON vol.ItemNumber = VI.InventoryNo AND VOL.Warehouse = VI.Warehouse WHERE Sku ='ENROLL'

4 个答案:

答案 0 :(得分:1)

有几种不同的方法可以做到这一点。这是使用条件聚合的一个选项:

select orderid
from yourtable
group by orderid
having max(case when sku = 'ENROLL' then 1 else 0 end) = 1  
   and max(case when sku != 'ENROLL' then 1 else 0 end) = 1  

答案 1 :(得分:1)

Sub creteUrls()
    Dim sld As Slide
    Dim sha As Shape
    Dim tempSh As Shape
    Dim i As Long
    Dim URL$
        For Each sld In ActivePresentation.Slides
            For Each sha In sld.Shapes
                For i = 4 To ActivePresentation.CustomXMLParts.Count
                    Set tempSh = sha
                        If tempSh.Name = CStr(ActivePresentation.CustomXMLParts(i).SelectNodes("webbrowser/name").item(1).Text) Then
                            URL = CStr(ActivePresentation.CustomXMLParts(i).SelectNodes("webbrowser/url").item(1).Text)
                            tempSh.OLEFormat.Object.Navigate URL, 4
                        End If
                    Set tempSh = Nothing
                Next i
            Next
        Next
    End Sub

说明:内部查询将为您提供在表格中多次出现的所有orderId。我们在查询的其余部分中所做的是选择所有等于' enroll' +出现在两次列表中

答案 2 :(得分:0)

您可以使用exists子查询:

select *
from tbl t
where exists (select 1 from tbl x where x.orderid = t.orderid and x.sku = 'ENROLL')
  and exists (select 1 from tbl x where x.orderid = t.orderid and x.sku <> 'ENROLL');

答案 3 :(得分:0)

到目前为止非常有趣的答案。我非常喜欢阅读它们。这是我的一个选择。

SELECT t1.*
FROM (SELECT * FROM tbl WHERE SKU = 'ENROLL') t1
INNER JOIN (SELECT * FROM tbl WHERE SKU != 'ENROLL') t2
ON t1.OrderID = t2.OrderID
GROUP BY t1.OrderID

我构建了两个表,其中所有订单至少有一个SKU ='ENROLL',另一个订单至少有一个SKU与'ENROLL'不同,我加入它们以获得......以及它们的横截面两个。这就是你要找的,对吗?