Move aggregate logic to separate function in SQL Server 2008 R2
<p>Please correct me if I'm using the wrong terminology or description, but I have business logic within an aggregate function (MIN and MAX). I wish to move to the logic to it's own function. I wish to do this so I don't have to make multiple changes in different locations when the client requests. I have the following</p>

<pre><code>SELECT DISTINCT 
        DATA_MONTH,
        DATA_DATE,
                CASE 
                  WHEN MIN(DATA_YEAR) = MAX(DATA_YEAR) 
                       AND MIN(DATA_YEAR) = @BEGIN_YR THEN 'Started' 
                  WHEN MIN(DATA_YEAR) = MAX(DATA_YEAR) 
                       AND MIN(DATA_YEAR) &lt;&gt; @CURRENTFY THEN 
                  CAST((MIN(DATA_YEAR) %100)-1 AS VARCHAR) 
                  + '/' 
                  + CAST(MIN(DATA_YEAR)%100 AS VARCHAR) 
                  WHEN MIN(DATA_YEAR) &lt;&gt; MAX(DATA_YEAR) 
                       AND MIN(DATA_YEAR) = @BEGIN_YR THEN 
                  'Started' + ' from ' 
                  + CAST((MAX(DATA_YEAR) %100)-1 AS VARCHAR) 
                  + '/' 
                  + CAST(MAX(DATA_YEAR)%100 AS VARCHAR) 
                  ELSE CAST((MIN(DATA_YEAR) %100)-1 AS VARCHAR) 
                       + '/' 
                       + CAST((MIN(DATA_YEAR) %100) AS VARCHAR) 
                       + ' through ' 
                       + CAST((MAX(DATA_YEAR) %100)-1 AS VARCHAR) 
                       + '/' 
                       + CAST(MAX(DATA_YEAR)%100 AS VARCHAR) 
                END                                 AS [STATUS] 
FROM   TABLE_A 
WHERE   DATA_MONTH IN ('03', '04')
        AND DATA_DATE = '01'
GROUP  BY DATA_MONTH, 
          DATA_DATE
</code></pre>

<p>Which I would like to change it to this:</p>

<pre><code>SELECT DISTINCT 
        DATA_MONTH,
        DATA_DATE,
        dbo.getStatus(DATA_YEAR) AS [STATUS]
FROM   TABLE_A 
WHERE   DATA_MONTH IN ('03', '04')
        AND DATA_DATE = '01'
GROUP  BY DATA_MONTH, 
          DATA_DATE
</code></pre>

<p>The logic for <code>getStatus()</code> has the case statements and can output:</p>

<pre><code>NULL
Started
15/16
Started from 15/16
15/16 through 16/17
</code></pre>

<p>My question is how can I restructure my logic to make this possible as I have a <code>GROUP BY</code> clause?</p>