

WITH 
  additional_visibility (project_id,Mid,
	emp_id,store_id,storetype_id,channel_id,chain_id,camera_allowed,visit_date,is_present,brand_id,
	display_id,Remarks,image_url,created_date,created_by)
as (

	SELECT '40148' AS ProjectId,ts.mid,  em.EmpId as EmpId                            
	, sm.StoreId,sm.storetypeid,sm.channelid,sm.chainid,case when sm.cameraallow=1 then 'Y'  ELSE 'N' END AS
	cameraallow , convert(varchar,sc.VisitDate,101)VisitDate,              
	case when ts.Present=0 then 'N' else 'Y' end Present,            
	isnull(MB.BrandId,'')[BrandId],       
	isnull(MD.DisplayId,'')[DisplayId],ISNULL(TS.REMARK,'')[Remark],            
	case when isnull(ts.ImageUrl,'')='' then '' else 'https://kimt1.parinaam.in/Upload/VisibilityImages/'+ts.ImageUrl            
	end as AdditionalImageUrl ,GETDATE(),'Pius'           
                 
	 from                            
	OneApp_KelloggsMT.dbo.T_AdditionalVisibility ts Inner join                            
	OneApp_KelloggsMT.dbo.T_StoreCoverage sc on ts.mid= sc.mid Inner Join                                        
	OneApp_KelloggsMT.dbo.vw_StoreDetail sm on sc.StoreId= sm.StoreId Inner Join                                        
	OneApp_KelloggsMT.dbo.vw_Employee_Detail Em on sc.EmpId= Em.EmpId             
	left  join             
	OneApp_KelloggsMT.dbo.Master_Brand MB ON TS.BrandId=MB.BrandId            
	LEFT JOIN            
	OneApp_KelloggsMT.dbo.Master_Display MD ON TS.DisplayId=MD.DisplayId                       
	Where Em.EmpName not like 'test%' and ts.Present=1   and sc.mid in ({mid_list})

)

select * from additional_visibility;