Monday, February 7, 2011

use of Outer Apply for joining function with existing query and Retriving collective information

1.Create a fuction like this

CREATE FUNCTION GetWarrantNotesDetails (@WarrantID varchar(22))   
   RETURNS @WarrantDetails Table   
     (  Remarks     varchar(1000)   
      , NoteDate  datetime   
      , AddedBy varchar(36)   
    )   
 AS   
   BEGIN   
       INSERT INTO @WarrantDetails (Remarks, NoteDate, AddedBy)   
        SELECT TOP(1) Remarks, NoteDate,[exec].cname   
        FROM warrant_notes iNNER JOIN [exec] ON   
       cast(warrant_notes.AddedBy as varbinary) = cast(dbo.[exec].cid as varbinary)      
      WHERE  WarrantID=@WarrantID AND AddedBy<>'Admin' ORDER BY NoteDate DESC   
     RETURN   
   END

2.call above mention fucntion in a query for Retriving its value
SELECT     SP_Case.warrantId, UPPER(Defendant_Master.DefendantName) AS 'Defendant Name', UPPER((client.Cname1)) AS 'Client Name',
                      CONVERT(varchar(20), StatusUpdateOn, 103) AS StatusUpdatedOn, UPPER(LEFT(STATUS_MASTER.STATUS_DESC, 255)) AS 'Status Description',
                      ISNULL(dbo.SP_Case.SpecialInstruction, '') AS 'SpecialInstruction', UPPER(ISNULL(report.DebtorType, ''))
                      AS DebtorType/*dbo.client.cltel1 AS 'Client Telephone1',dbo.client.cltel2 AS 'Client Telephone2',ISNULL(dbo.client.clmobile,'') AS 'Client Mobile'*/ ,
                      Defendant_Phone.Phone1 AS 'DebtorPhone1', defendant_phone.Phone2 AS 'DebtorPhone2', defendant_phone.Mobile AS 'DebtorMobile',
                      Defendant_Address.PCode,
                      ISNULL((dbo.Defendant_Address.Add1 + dbo.Defendant_Address.Add2 + dbo.Defendant_Address.Add3 + dbo.Defendant_Address.Add4), '')
                      AS 'Defendant Address', SP_Case.clientrefNo AS 'Client Reference Number', CASE WHEN CONVERT(VARCHAR(20), ISNULL(report.FirstVisitDT, ''), 103)
                      = '01/01/1900' THEN '' ELSE CONVERT(VARCHAR(20), ISNULL(report.FirstVisitDT, ''), 103) END AS 'FirstVisitDT', ISNULL(dbo.report.ArFirstVisit, '')
                      AS 'ArFirstVisit', CASE WHEN CONVERT(VARCHAR(20), ISNULL(report.SecondVisitDT, ''), 103) = '01/01/1900' THEN '' ELSE CONVERT(VARCHAR(20),
                      ISNULL(report.SecondVisitDT, ''), 103) END AS 'SecondVisitDT', ISNULL(dbo.report.ArSecondVisit, '') AS 'ArSecondVisit',
                      CASE WHEN CONVERT(VARCHAR(20), ISNULL(report.ThirdVisitDT, ''), 103) = '01/01/1900' THEN '' ELSE CONVERT(VARCHAR(20),
                      ISNULL(report.ThirdVisitDT, ''), 103) END AS 'ThirdVisitDT', ISNULL(dbo.report.ArThirdVisit, '') AS 'ArThirdVisit', ISNULL(dbo.report.Recommendation, '')
                      AS 'RID', ISNULL(DebtorIncomeExpenditure.DPSalary, '') AS 'Defendant Salary', WN.Remarks, CONVERT(VARCHAR(20), WN.Notedate, 103)
                      AS 'NoteDate', UPPER(WN.AddedBy) AS 'Added By', dbo.GetBalanceOutStanding(sp_case.Warrantid) AS 'Balance Outstanding',
                      CONVERT(VARCHAR(20), dbo.SP_Case.EntryDate, 103) AS 'Entry Date', UPPER(ISNULL(dbo.report.mnuTypeOfPremises, '')) AS 'Class',
                      UPPER(ISNULL(report.OfficerName, '')) AS 'Officer Name', ISNULL(t .PaymentAmount, 0) AS 'Payment Amount', ISNULL(t .Frequency, '') AS 'Frequency',
                      ISNULL(t .LastPaymentDate, '') AS 'Last Payment Date', ISNULL(t .PlanCreateDate, '') AS 'Plan Create Date', ISNULL(INV.DESCRIPTION, '')
                      AS 'DESCRIPTION', ISNULL(INV.Valuation, '') AS 'Valuation', LEFT(dbo.STATUS_MASTER.STATUS_CODE, 255) AS 'Status Code'
FROM         SP_Case OUTER APPLY GetWarrantNotesDetails(dbo.SP_Case.WarrantID) AS WN OUTER APPLY GetInventory(dbo.SP_Case.WarrantID)
                      AS INV INNER JOIN
                      Defendant_Master ON cast(SP_Case.WarrantID AS varbinary) = cast(Defendant_Master.WarrantID AS varbinary) INNER JOIN
                      client ON cast(client.CID AS varbinary) = cast(SP_Case.CClientId AS varbinary) INNER JOIN
                      Defendant_Address ON cast(Defendant_Address.Def_Id AS varbinary) = cast(Defendant_Master.CID AS varbinary) INNER JOIN
                      Defendant_Phone ON cast(Defendant_Phone.Def_Id AS varbinary) = cast(Defendant_Master.CID AS varbinary) INNER JOIN
                      STATUS_MASTER ON cast(STATUS_MASTER.STATUS_ID AS varbinary) = cast(SP_Case.Status AS varbinary) LEFT OUTER JOIN
                      report ON cast(report.WarrantID AS varbinary) = cast(SP_Case.WarrantID AS varbinary) LEFT OUTER JOIN
                      DebtorIncomeExpenditure ON cast(DebtorIncomeExpenditure.WarrantRefNo AS varbinary) = cast(dbo.SP_Case.WarrantID AS varbinary)
                      LEFT OUTER JOIN
                          (SELECT     Paymentplan.Warrantid, isnull(cast(dbo.Payplan_Installment.InstallmentAmount AS decimal(8, 2)), 0) AS PaymentAmount,
                                                   ISNULL(Payplan_Installment.Frequency, '') AS Frequency, CASE WHEN CONVERT(VARCHAR(20), ISNULL(Payplan_Installment.DateTo, ''),
                                                   103) = '01/01/1900' THEN '' ELSE CONVERT(VARCHAR(20), ISNULL(Payplan_Installment.DateTo, ''), 103) END AS LastPaymentDate,
                                                   CASE WHEN CONVERT(VARCHAR(20), ISNULL(Payplan_Installment.CreatedDate, ''), 103)
                                                   = '01/01/1900' THEN '' ELSE CONVERT(VARCHAR(20), ISNULL(Payplan_Installment.CreatedDate, ''), 103) END AS PlanCreateDate
                            FROM          Paymentplan INNER JOIN
                                                   Payplan_Installment ON cast(Paymentplan.Pid AS varbinary) = cast(Payplan_Installment.pid AS varbinary)
                            WHERE      isactive = 'Y'/*Paymentplan.Warrantid like 'SPC%' and    */ ) t ON cast(t .WarrantId AS varbinary) = cast(SP_Case.WarrantID AS varbinary)

No comments:

Post a Comment