Select * From ( Select top 1 D.Rowid, Case when isnull(B.ApplicantName,'') <> '' then B.ApplicantName else A.Name End as ApplicantName ,Case when isnull(B.RollNo,'') <> '' then B.RollNo else A.RollNo End as RollNo ,Case when isnull(B.ApplicationNo,'') <> '' then B.ApplicationNo else A.AppId End as ApplicationNo ,Case when isnull(B.FatherName,'') <> '' then B.FatherName else A.FathersName End as FatherName ,Case when isnull(B.Category,'') <> '' then B.Category else Case When CategoryApplied = 'Other Backward Class' then 'OBC' When CategoryApplied = 'Scheduled Caste' then 'SC' When CategoryApplied = 'Scheduled Tribe' then 'ST' When CategoryApplied = 'Unreserved' then 'Unreserved' else '0' End End as Category ,Case when isnull(B.Gender,'') <> '' then B.Gender else A.Gender End as Gender ,CONVERT(VARCHAR(10),Case when isnull(B.DOB,'') <> '' then B.DOB else D.DOB End, 103) as DOB --,CONVERT(VARCHAR(10), A.DOB, 105) AS DOB ,Case when isnull(B.MobileNo,'') <> '' then B.MobileNo else D.MobileNo End as MobileNo ,replace(Case when isnull(B.AlternateNo,'') <> '' then B.AlternateNo else Y.AlternateMobile End,'-','') as AlternateNo ,Case when isnull(B.EmailId,'') <> '' then B.EmailId else Y.EmailId End as EmailId ,Case when isnull(B.BarCouncilID,'') <> '' then B.BarCouncilID +'_'+ C.NewEnrollmentNo +'_'+ C.BarPrefix else '0' End as BarCouncilID ,replace(Case when isnull(B.EnrollmentNo,'') <> '' then B.EnrollmentNo else A.EnrollmentNo End,'-','') as EnrollmentNo,B.ProfileId ,Case when isnull(B.PhotographPath,'') <> '' then B.PhotographPath else '/AIBE_Image/Photograph/' + D.OldRollNo + '.jpeg' End as PhotographPath , Case when isnull(B.SignaturePath,'') <> '' then B.SignaturePath else '/AIBE_Image/Signature/'+ D.OldRollNo + '.jpeg' End as SignaturePath , B.CertificatePath ,D.KeyField ,C.BarCouncil,isnull(B.ModifiedOn,B.CreatedOn) as 'LastActionDate' --Select COUNT(1) from AIBE_DB.dbo.AIBEResultTB A Inner Join AIBE_DB.dbo.AIBEAdmitCardXX D on A.RollNo = D.RollNo --Inner Join ( -- --Select B.Photograph, B.Signature,B.KeyField -- --From AIBE_DB.dbo.ApplicantImageDataTB B -- --where B.IsActive = 1 -- Select '/AIBE_Image/Photograph/' + A.OldRollNo + '.jpeg' as Photograph, '/AIBE_Image/Signature/'+ A.OldRollNo + '.jpeg' as 'Signature', A.KeyField, A.AppId -- From AIBE_DB.dbo.AIBE_DB.dbo.AIBEAdmitCardXX A -- where A.IsActive = 1 --) E on D.KeyField = E.KeyField inner join ( Select Max(RowID) as RowID, AppId--, AlternateMobile, EmailId From AIBE_DB.dbo.ApplicantDataTB Where IsActive = 1 Group By AppID ) X on A.AppId = X.AppID Inner Join AIBE_DB.dbo.ApplicantDataTB Y on X.AppID = Y.AppID and X.RowID = Y.RowId left join AIBE_DB.dbo.EnrollmentDataTB B on B.RollNo = A.RollNo and B.ApplicationNo = A.AppId and B.KeyField = B.KeyField and B.IsActive = 1 Left Join AIBE_MasterDB.dbo.BarCouncilMasterTB C on B.BarCouncilID = C.BarId where D.IsActive=1 and D.KeyField = @KeyField union All Select top 1 A.Rowid, Case when isnull(B.ApplicantName,'') <> '' then B.ApplicantName else A.ApplicantName End as ApplicantName ,Case when isnull(B.RollNo,'') <> '' then B.RollNo else A.RollNo End as RollNo ,Case when isnull(B.ApplicationNo,'') <> '' then B.ApplicationNo else A.ApplicationNo End as ApplicationNo ,Case when isnull(B.FatherName,'') <> '' then B.FatherName else A.FatherName End as FatherName ,Case when isnull(B.Category,'') <> '' then B.Category else A.Category End as Category ,Case when isnull(B.Gender,'') <> '' then B.Gender else A.Gender End as Gender ,CONVERT(VARCHAR(10),Case when isnull(B.DOB,'') <> '' then B.DOB else A.DOB End, 103) as DOB --,CONVERT(VARCHAR(10), A.DOB, 105) AS DOB ,Case when isnull(B.MobileNo,'') <> '' then B.MobileNo else A.MobileNo End as MobileNo ,replace(Case when isnull(B.AlternateNo,'') <> '' then B.AlternateNo else A.AlternateNo End,'-','') as AlternateNo ,Case when i