2011-07-11 74 views
0

我有3個表(公司簡介,IndustryTable,MainTable)加入3個表

公司簡介

CompanyCode  CompanyName  IndustryCode 
AAAA    Company A  3 
BBBB    Company B  1 
CCCC    Company C  4 
DDDD    Company D  1 
EEEE    Company E  1 
GGGG    Company F  2 

IndustryTable

IndustryCode  IndustryName  status 
1    Manufacturing ACTIVE 
2    Sales   ACTIVE 
3    Logistics  ACTIVE 
4    Energy   DEACTIVATED 

MainTable

CompanyCode field2 
AAAAA  SampleRecord1 
AAAAA  SampleRecord2 
DDDDD  SampleRecord3 
CCCCC  SampleRecord4 
EEEEE  SampleRecord5 

現在我需要一個查詢來獲取所有RecordCountMainTable集團通過IndustryTable.IndustryCode(ACTIVE只),所以上面的例子應該給下面的輸出

IndustryCode  IndustryName  RecordCount (Explanation) 
1    Manufacturing 2    -Record 3 and 5 
2    Sales   0    
3    Logistics  2    -Record 1 and 2 

記錄4(CCCCC)不會是顯示由於IndustryTable.IndustryCode 4被停用

回答

0

喜歡的東西

select industrycode, industryname, count(*) as total from companyprofile t1 join industry table t2 join maintable t3 where t1.companycode=t3.companycode and t1.industrycode=t2.industrycode and t2.status="ACTIVE" group by industrycode