我正在尝试查找在报告中出现过一次或更少的项目。我知道要查找每个项目出现了多少次,我使用它。
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 次的那些。