SQL Server 2012, everything needs to be a SELECT statement with no procedures (platform limitations).
I have a table like:
<code>| Field A | Field B | Number Field | Row Num |
----------------------------------------------
| A1 | X | 19 | 1 |
| B1 | | 20 | 2 |
| | X | 23 | 3 |
| A1 | | 45 | 4 |
| | Y | 13 | 5 |
| C1 | Z | 23 | 6 |
| C2 | Z | 24 | 7 |
| | Z | 12 | 8 |
| C2 | | 4 | 9 |
| C2 | J | 3 | 10 |
| C4 | J | 6 | 11 |
| C1 | J | 5 | 12 |
</code>
<code>| Field A | Field B | Number Field | Row Num |
----------------------------------------------
| A1 | X | 19 | 1 |
| B1 | | 20 | 2 |
| | X | 23 | 3 |
| A1 | | 45 | 4 |
| | Y | 13 | 5 |
| C1 | Z | 23 | 6 |
| C2 | Z | 24 | 7 |
| | Z | 12 | 8 |
| C2 | | 4 | 9 |
| C2 | J | 3 | 10 |
| C4 | J | 6 | 11 |
| C1 | J | 5 | 12 |
</code>
| Field A | Field B | Number Field | Row Num |
----------------------------------------------
| A1 | X | 19 | 1 |
| B1 | | 20 | 2 |
| | X | 23 | 3 |
| A1 | | 45 | 4 |
| | Y | 13 | 5 |
| C1 | Z | 23 | 6 |
| C2 | Z | 24 | 7 |
| | Z | 12 | 8 |
| C2 | | 4 | 9 |
| C2 | J | 3 | 10 |
| C4 | J | 6 | 11 |
| C1 | J | 5 | 12 |
And I want a result like:
<code>| Field A | Field B | Number Field Sum |
----------------------------------------
| A1 | X | 87 (rows 1, 3, 4)
| B1 | | 20 (row 2)
| | Y | 13 (row 5)
| C1 | Z | 77 (rows 6-12)
</code>
<code>| Field A | Field B | Number Field Sum |
----------------------------------------
| A1 | X | 87 (rows 1, 3, 4)
| B1 | | 20 (row 2)
| | Y | 13 (row 5)
| C1 | Z | 77 (rows 6-12)
</code>
| Field A | Field B | Number Field Sum |
----------------------------------------
| A1 | X | 87 (rows 1, 3, 4)
| B1 | | 20 (row 2)
| | Y | 13 (row 5)
| C1 | Z | 77 (rows 6-12)
For rows 6-7, I don’t care which combination of Field A
and Field B
is in the result set, only that all of the rows that are associated to each other are aggregated together.
I only need the sum or any other aggregate function, I don’t need text that says ‘(rows 6-12)’, that was just for explanation purposes.