SELECT dbo.QMS.QMSCode, dbo.QMS.QMSSerial, dbo.QMS.model, dbo.QMS.controlType, dbo.QMS.connectionType, dbo.QMS.generateType, dbo.QMS.isCamera, dbo.QMS.centralMonitorRowCount, dbo.QMS.TV_LCD, dbo.QMS.callNumber, dbo.QMS.monitorNumber, dbo.QMS.centralMonitorNumber, dbo.QMS.description, dbo.QMS.lastUsedBranchCode, dbo.QMS.lastUsedCompanyCode, dbo.QMS.warrantyStartDate, dbo.QMS.warrantyEndDate, dbo.QMS.warrantyType, dbo.QMS.lastCheckDate, dbo.QMS.isActive, dbo.Branch.name AS BranchName, dbo.Company.name AS CompanyName, (Case When ((Select Count(QMSCode) From DefectReport D Where D.productCode=QMS.QMSCode AND D.Done=0)>0) Then Cast(0 As bit) Else Cast(1 As Bit) END) As DefectDone, (Case When ((Select Count(QMSCode) From [Order] O Where O.productCode=QMS.QMSCode AND O.Done=0) > 0) Then Cast(0 As bit) Else Cast(1 As Bit) END) As OrderDone FROM dbo.QMS LEFT OUTER JOIN dbo.Company ON dbo.QMS.lastUsedCompanyCode = dbo.Company.companyCode LEFT OUTER JOIN dbo.Branch ON dbo.QMS.lastUsedBranchCode = dbo.Branch.branchCode LEFT OUTER JOIN dbo.[Order] ON dbo.QMS.QMSCode = dbo.[Order].productCode CROSS JOIN dbo.DefectReport group by dbo.QMS.QMSCode, dbo.QMS.QMSSerial, dbo.QMS.model, dbo.QMS.controlType, dbo.QMS.connectionType, dbo.QMS.generateType, dbo.QMS.isCamera, dbo.QMS.centralMonitorRowCount, dbo.QMS.TV_LCD, dbo.QMS.callNumber, dbo.QMS.monitorNumber, dbo.QMS.centralMonitorNumber, dbo.QMS.description, dbo.QMS.lastUsedBranchCode, dbo.QMS.lastUsedCompanyCode, dbo.QMS.warrantyStartDate, dbo.QMS.warrantyEndDate, dbo.QMS.warrantyType, dbo.QMS.lastCheckDate, dbo.QMS.isActive, dbo.Branch.name, dbo.Company.name