我有三张桌子(CompanyProfile,IndustryTable,MainTable)
CompanyProfile
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 2IndustryTable
IndustryCode IndustryName status
1 Manufacturing ACTIVE
2 Sales ACTIVE
3 Logistics ACTIVE
4 Energy DEACTIVATEDMainTable
CompanyCode field2
AAAAA SampleRecord1
AAAAA SampleRecord2
DDDDD SampleRecord3
CCCCC SampleRecord4
EEEEE SampleRecord5现在,我需要一个查询来从MainTable Group IndustryTable.IndustryCode获得所有的IndustryTable.IndustryCode(仅为活动的),因此上面的示例应该提供以下输出
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已停用
发布于 2011-07-11 11:40:21
有点像
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
https://stackoverflow.com/questions/6649132
复制相似问题