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

しかし、別のクエリ内にネストして、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句は必要なものです。これは句に似ていますがWHEREGROUP 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 に答える