我正在尝试查找报告中出现过一次或更少次数的项目。我知道要找到每个项目有多少次,我会用它。
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
只显示那些在采购订单上出现过一次的那些。
答案 0 :(得分:3)
HAVING
子句是您需要的 - 它就像WHERE
子句,但适用于GROUP BY
产生效果:
Select Id, count(othercolumn)
from sometable
where somecolumn = something
group by Id
having (count(somecolumn) < 2)
答案 1 :(得分: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
答案 2 :(得分: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