SELECT DATE_FORMAT(plo.Date,'%Y/%m/%d %h:%m:%s') MySQLDate,(SELECT centre FROM centre_master WHERE centreid=plo.TestCentreID) centre,lt.LedgerTransactionNo, plo.Test_ID,plo.SubCategoryName dept,plo.itemname,plo.Investigation_ID,plo.BarcodeNo,CONCAT(lt.PName,'/', lt.Age,'/',lt.Gender) PName,CONCAT( lt.Age,'/',lt.Gender)Age, plo.LabOutsrcID LabOutsrcID, plo.LabOutsrcName LabOutsrcName, plo.LabOutSrcRate LabOutSrcRate, ifnull(concat(io.TAT,' ',TATType),'') TAT,if(IsFileRequired=0,'No','Yes') IsFileRequired, 'OutSourced' Status,plo.LabOutSrcBy, DATE_FORMAT(plo.LabOutSrcDate,'%d-%b-%y %h:%i%p') LabOutSrcDate,DATE_FORMAT(plo.Date,'%d-%b-%y %h:%i%p') RegDate FROM patient_labinvestigation_opd plo INNER JOIN f_ledgertransaction lt ON lt.LedgerTransactionID=plo.LedgerTransactionID AND plo.IsActive=1 and IFNULL(plo.LabOutsrcID,0)<>0 and plo.Date>= @fromDate AND plo.Date<= @todate and ( plo.TestCentreID in ( select ca.CentreAccess from centre_access ca where ca.Centreid=@ToCentreID ) or plo.TestCentreID=@ToCentreID) LEFT JOIN investigations_outsrclab io ON io.CentreID=plo.TestCentreID AND io.Investigation_ID=plo.Investigation_ID UNION ALL SELECT DATE_FORMAT(plo.Date,'%Y/%m/%d %h:%m:%s') MySQLDate,(SELECT centre FROM centre_master WHERE centreid=plo.TestCentreID) centre,lt.LedgerTransactionNo, plo.Test_ID,plo.SubCategoryName dept,plo.itemname,plo.Investigation_ID,plo.BarcodeNo,CONCAT(lt.PName,'/', lt.Age,'/',lt.Gender) PName,CONCAT( lt.Age,'/',lt.Gender)Age, IF(IFNULL(plo.LabOutsrcID,'0')='0',io.OutSrcLabID,plo.LabOutsrcID) LabOutsrcID, IF(IFNULL(plo.LabOutsrcID,'0')='0',io.OutSrcLabname,plo.LabOutsrcName) LabOutsrcName, IF(IFNULL(plo.LabOutsrcID,'0')='0',io.OutsourceRate,plo.LabOutSrcRate) LabOutSrcRate, ifnull(concat(io.TAT,' ',TATType),'') TAT,if(IsFileRequired=0,'No','Yes') IsFileRequired, IF(IFNULL(plo.LabOutsrcID,'0')='0','Pending','OutSourced') Status,plo.LabOutSrcBy, DATE_FORMAT(plo.LabOutSrcDate,'%d-%b-%y %h:%i%p') LabOutSrcDate,DATE_FORMAT(plo.Date,'%d-%b-%y %h:%i%p') RegDate FROM patient_labinvestigation_opd plo INNER JOIN sample_logistic sl ON plo.BarcodeNo=sl.BarcodeNo AND sl.testid = plo.Test_ID AND sl.Status='OutSource' and ( sl.ToCentreID in ( select ca.CentreAccess from centre_access ca where ca.Centreid=@ToCentreID ) or sl.ToCentreID =@ToCentreID) INNER JOIN investigations_outsrclab io ON io.CentreID=sl.ToCentreID AND io.Investigation_ID=plo.Investigation_ID INNER JOIN f_ledgertransaction lt ON lt.LedgerTransactionID=plo.LedgerTransactionID AND plo.IsActive=1 and IFNULL(plo.LabOutsrcID,0)=0 AND plo.Approved=0 where sl.ReceivedDate >= @fromDate AND sl.ReceivedDate <= @todate order by MySQLDate;