title: Create SQL Function/Procedure from 3 Tables
body: <p>I have 3 tables, category, Resource Human and HR Plan.</p>
  
  <p><a href="https://i.stack.imgur.com/nhxuC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nhxuC.png" alt="enter image description here"></a></p>
  
  <p>I want to calculate The amount Group by Category</p>
  
  <p>I created this SQL query:</p>
  
  <pre><code>select 
      CostCategory.CostCategory, sum(isnull(HRPplan.Amount, 0))
  from 
      CostCategory
  left join 
      HRPplan on HRPplan.Month between 1 and 3 
              and HRPplan.Year = 2019 
              and HRPplan.IDRPH in (select HumanResource.IDRPH 
                                    from HumanResource
                                    where HumanResource.IDCostCategory = CostCategory.IDCostegory)
  group by  
      CostCategory.CostCategory
  </code></pre>
  
  <p>and it returns a result, but I have another 2 tables exactly look like Human Resource &amp; HPlan, to use it to calculate the amount, so if I use the same code mentioned above the query performance not good. And I don't how to use the function/procedure in SQL, since that the year &amp; the month will be inserted as parameter.</p>