>CallReports Table > > CallReport_Id etc... >1 >2 >3 > > > >CallReport_Users Table > >CallReport_Id UserId >1 A >2 A >2 B >2 C >3 C > > >CallReport_Companys Table > >CallReport_id Company_id >1 G >1 H >2 M >3 K >>
>CallReport_id UserId Company_id >1 A 'Multiple' >2 'Multiple' M >3 C 'Multiple' >>
Select R.*, case when U.cntUsers > 1 then 'Multiple' else UserID end as UserId, case when C.cntCompanies>1 then 'Multiple' else Company_id end as Company_id from CallReports R LEFT JOIN (select CallReport_id, min(UserId) as UserId, count(UserID) as cntUsers FROM CallReport_Users GROUP BY CallReport_ID) U ON R.CallReport_id = U.CallReport_Id LEFT JOIN (select CallReport_id, min(Company_Id) as Company_Id, count(Company_ID) as cntCompanies FROM CallReport_Companys GROUP BY CallReport_ID) C ON R.CallReport_id = C.CallReport_Id