title: How to run 2 Queries inside a stored Function
body: <p>I'm New to Functions in SQL and am hoping for some Guidance.  I have a Function in SQL that i'd like to add another query to, but I can't quite figure out how to make it happen.</p>
  
  <p>My current function is:</p>
  
  <pre><code>ALTER FUNCTION dbo.CountVisits
  (@startdate Datetime,
   @enddate Datetime,
  )
  RETURNS TABLE
  AS
     RETURN       
         SELECT     
            COUNT(DISTINCT Invoice_Number) AS TotalVisits
         FROM         
            Invoice_Tb
         WHERE 
            dtcreated &gt;= @startdate 
            AND dtcreated &lt;= @enddate 
            AND Service_Id = '1'
  </code></pre>
  
  <p>Current output:</p>
  
  <pre><code>Total Visits
  ------------
      15
  </code></pre>
  
  <p>This will return "TotalVisists" where the service ID is 1. Now, let's say I want to also include "Total Visits" I know I can do two queries, inside a query. But is there a way to break this out within the Function to run another query, and put it all together as part of the output?</p>
  
  <p>My second query would be something like:</p>
  
  <pre><code> SELECT     
       COUNT(DISTINCT Invoice_Number) AS TotalVisitsALL
   FROM
       Daily_TB
   WHERE
       dtcreated &gt;= @startdate 
       AND dtcreated &lt;= @enddate 
  </code></pre>
  
  <p>Output I want:</p>
  
  <pre><code>Total Visits     TotalVisitsALL
  --------------------------------
       15                300
  </code></pre>