sql server 2008 - SQL Count in Where clause -


i'm trying find items have shown 1 time or less on report. know find how many times each item has apepared, use this.

select count(vp.vendorpartid) purchasing.purchaseorder po (nolock)     inner join dbo.tblvendor v (nolock)         on po.vendorid=v.vendorid     inner join purchasing.purchaseorderitem poi (nolock)         on po.purchaseorderid=poi.purchaseorderid     inner join purchasing.vendorpart vp (nolock)         on poi.vendorpartid=vp.vendorpartid v.producttypeid=4 group po.purchaseorderid 

but tried nest within query able set must appear 1 time or less, , says there's error because

"subquery returned more 1 value. not permitted when subquery follows =, !=, <, <= , >, >= or when subquery used expression."

i did this, i'm guessing pretty wrong, haha.

select vp.vendorpartid,vp.vendorpartdescription purchasing.purchaseorder po (nolock)     inner join dbo.tblvendor v (nolock)         on po.vendorid=v.vendorid     inner join purchasing.purchaseorderitem poi (nolock)         on po.purchaseorderid=poi.purchaseorderid     inner join purchasing.vendorpart vp (nolock)         on poi.vendorpartid=vp.vendorpartid (         select count(vp.vendorpartid)         purchasing.purchaseorder po (nolock)             inner join dbo.tblvendor v (nolock)                 on po.vendorid=v.vendorid             inner join purchasing.purchaseorderitem poi (nolock)                 on po.purchaseorderid=poi.purchaseorderid             inner join purchasing.vendorpart vp (nolock)                 on poi.vendorpartid=vp.vendorpartid         v.producttypeid=4         group po.purchaseorderid         ) < 2 group vp.vendorpartid,vp.vendorpartdescription 

desired results be

vendorpartid  vendorpartdescription   001           name 1                  002           name 2                  003           name 3                  

it show had appeared 1 time on purchase orders.

the having clause need--it's where clause, applied group by

something effect of:

select id, count(othercolumn) sometable somecolumn = group id having (count(somecolumn) < 2) 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -