1

我正在尝试查找在报告中出现过一次或更少的项目。我知道要查找每个项目出现了多少次,我使用它。

select COUNT(VP.VendorPartID)
from Purchasing.PurchaseOrder PO with (nolock)
    inner join dbo.tblVendor V with (nolock)
        on PO.VendorID=V.VendorID
    inner join Purchasing.PurchaseOrderItem POI with (nolock)
        on PO.PurchaseOrderID=POI.PurchaseOrderID
    inner join Purchasing.VendorPart VP with (nolock)
        on POI.VendorPartID=VP.VendorPartID
where V.ProductTypeID=4
group by PO.PurchaseOrderID

但我试图将它嵌套在另一个查询中,以便能够设置它必须出现 1 次或更少,它说有一个错误,因为

“子查询返回的值超过 1 个。当子查询跟随 =、!=、<、<=、>、>= 或将子查询用作表达式时,这是不允许的。”

我这样做了,我猜这很可能是错误的,哈哈。

select VP.VendorPartID,VP.VendorPartDescription
from Purchasing.PurchaseOrder PO with (nolock)
    inner join dbo.tblVendor V with (nolock)
        on PO.VendorID=V.VendorID
    inner join Purchasing.PurchaseOrderItem POI with (nolock)
        on PO.PurchaseOrderID=POI.PurchaseOrderID
    inner join Purchasing.VendorPart VP with (nolock)
        on POI.VendorPartID=VP.VendorPartID
where (
        select COUNT(VP.VendorPartID)
        from Purchasing.PurchaseOrder PO with (nolock)
            inner join dbo.tblVendor V with (nolock)
                on PO.VendorID=V.VendorID
            inner join Purchasing.PurchaseOrderItem POI with (nolock)
                on PO.PurchaseOrderID=POI.PurchaseOrderID
            inner join Purchasing.VendorPart VP with (nolock)
                on POI.VendorPartID=VP.VendorPartID
        where V.ProductTypeID=4
        group by PO.PurchaseOrderID
        ) < 2
group by VP.VendorPartID,VP.VendorPartDescription

期望的结果是

VendorPartID  VendorPartDescription  
001           name 1                 
002           name 2                 
003           name 3                 

它只会显示那些在采购订单上出现过 1 次的那些。

4

3 回答 3

3

HAVING子句就是你所需要的——它就像一个子句WHERE,但适用于一个GROUP BY

大意是:

Select Id, count(othercolumn)
from sometable
where somecolumn = something
group by Id
having (count(somecolumn) < 2)
于 2013-08-28T20:48:34.450 回答
1

如果这真的适合您的计数:

select COUNT(VP.VendorPartID)
from Purchasing.PurchaseOrder PO with (nolock)
    inner join dbo.tblVendor V with (nolock)
        on PO.VendorID=V.VendorID
    inner join Purchasing.PurchaseOrderItem POI with (nolock)
        on PO.PurchaseOrderID=POI.PurchaseOrderID
    inner join Purchasing.VendorPart VP with (nolock)
        on POI.VendorPartID=VP.VendorPartID
where V.ProductTypeID=4
group by PO.PurchaseOrderID`

然后只需添加一个having子句:

select PO.PurchaseOrderID, COUNT(VP.VendorPartID)
from Purchasing.PurchaseOrder PO with (nolock)
    inner join dbo.tblVendor V with (nolock)
        on PO.VendorID=V.VendorID
    inner join Purchasing.PurchaseOrderItem POI with (nolock)
        on PO.PurchaseOrderID=POI.PurchaseOrderID
    inner join Purchasing.VendorPart VP with (nolock)
        on POI.VendorPartID=VP.VendorPartID
where V.ProductTypeID=4
group by PO.PurchaseOrderID
having COUNT(VP.VendorPartID) <= 1
于 2013-08-28T20:45:44.803 回答
0

试试这个,它可以让您根据计数获取详细信息

select * from
(select VP.VendorPartID,VP.VendorPartDescription,COUNT(VP.VendorPartID) as PartCount
  from Purchasing.PurchaseOrder PO with (nolock)
    inner join dbo.tblVendor V with (nolock)
      on PO.VendorID=V.VendorID
    inner join Purchasing.PurchaseOrderItem POI with (nolock)
      on PO.PurchaseOrderID=POI.PurchaseOrderID
    inner join Purchasing.VendorPart VP with (nolock)
      on POI.VendorPartID=VP.VendorPartID
group by
  VP.VendorPartID,VP.VendorPartDescription) as qCounts
where qCounts.PartCount < 2
于 2013-08-28T20:48:43.773 回答