组项,过滤它们并用sql计数

时间:2015-07-24 13:14:01

标签: sql sql-server tsql sql-server-2012

我有一个sql查询连接FOUR表返回过滤后的数据。

这些是我的表结构:

ProductBase

Id
ProductNumber

ProductDocuments

Id
Lang (like pl)
ProductBase_FK

ProductCountry(N到M)

Product_FK
Country_FK

国家

Id
CountryName (like Tschechien)

QUERY

Select MATNR, PRODUCTNAME, PRODUCTBRAND, CountryName    , Lang FROM ProductBase prod 
inner join ProductDocuments doc ON prod.ID = doc.ProductBase_FK
inner join ProductCountryNM prodcountry ON prod.ID = prodcountry.Product_FK
inner join Countries country ON prodcountry.Country_FK = country.ID
where doc.DocType = 'xxx'

这是查询的结果:

MATNR   PRODUCTNAME PRODUCTBRAND    CoutryName  Lang
5518543092  BigCell Wutera          AT          #Undefined
5518543092  BigCell Wutera          AT          sk
3672367236  LowCell Wutera          AT          cs
3672367236  LowCell Wutera          AT          pl

如您所见,我从CoutryName AND Lang(uage)Field获得所有组合。

如何获得产品的COUNT(标识符为MATNR),其中至少 ONE Lang(uage)定义为' #Undefined'。这意味着"有多少产品还有不完整的国家"?

注意:

  • 如果只有一个产品具有相同的MATNR' #Undefined'然后它被视为不完整。

  • 如果存在一个产品4次,同一个MATNR具有' #Undefined'然后它被视为不完整。

样品

不完全

MATNR   PRODUCTNAME PRODUCTBRAND    CoutryName  Lang
5518543092  BigCell Wutera          AT          #Undefined
5518543092  BigCell Wutera          AT          at

MATNR   PRODUCTNAME PRODUCTBRAND    CoutryName  Lang
5518543092  BigCell Wutera          AT          #Undefined
5518543092  BigCell Wutera          AT          #Undefined

完整

MATNR   PRODUCTNAME PRODUCTBRAND    CoutryName  Lang
5518543092  BigCell Wutera          DE          de
5518543092  BigCell Wutera          FR          fr

最低一个' #Undefined'在Lang for a Product中,它不完整: - )

2 个答案:

答案 0 :(得分:4)

我不知道MS SQL Server 2012,但试试这个:

$('#my-cprogress').cprogress({
           percent: 10, // starting position
           img1: 'v1.png', // background
           img2: 'v2.png', // foreground
           speed: 200, // speed (timeout)
           PIStep : 0.05, // every step foreground area is bigger about this val
           limit: 20, // end value
           loop : false, //if true, no matter if limit is set, progressbar will be running
           showPercent : true, //show hide percent
           onInit: function(){console.log('onInit');},
           onProgress: function(p){console.log('onProgress',p);}, //p=current percent
           onComplete: function(p){console.log('onComplete',p);}
      });

答案 1 :(得分:0)

with x as (Select MATNR, PRODUCTNAME, PRODUCTBRAND, CountryName, Lang,
case when Lang = '#Undefined' then 1 else 0 end as cnt
FROM ProductBase prod
inner join ProductDocuments doc ON prod.ID = doc.ProductBase_FK
inner join ProductCountryNM prodcountry ON prod.ID = prodcountry.Product_FK
inner join Countries country ON prodcountry.Country_FK = country.ID)
select count(distinct matnr) 
from x where cnt = 1