<p>I have a group by clause in a sql statement and need to use an aggregate function to minus all the values in each group instead of adding like the Sum() function.</p>

<p>i.e.  </p>

<pre><code>SELECT Sum(A) 
FROM (  
  SELECT 2 AS A
  UNION 
  SELECT 1) AS t1
</code></pre>

<p>..so will evaluate 2+1 and return 3.</p>

<p>I need some way of doing 2-1 to return 1.</p>

<p>Hope this makes sense.  Only way I can think of doing this would be to use CLR integration to make my own aggregate function.</p>

<p>Any other ideas?</p>

How will you identify the item to be subtracted from?
Once that's been identified it's a
```
SUM()
```
multiplied by
```
-1
```
and then added to that value.
Edit:
If it's the first value to be taken as the subtracted from then take that value, double it, then take away the sum of all the values. (Doubling it cancels out the effect of including it in the sum of all the values.)
```
```
select top 1 @var = [value]
from myTable 
order by [some condition] 

select @minused = (2 * @var)  - sum([value]) 
from myTable
```
```