Query to know who paid more than 10k I have a table with payment informations, like this:

ID COSTUMER_ID DATEPAID     AMOUNT
1    1        2012-01-01      50
2    1        2012-02-01      100
3    1        2012-03-01      70
4    2        2012-03-01     6000
4    2        2012-09-01     3000
4    2        2014-04-01     8000
5    3        2013-03-01     9000
6    3        2013-08-01     3000


etc.
I need to know wich costumers paid more than 10k in a 12 months period. (in the exemple, only costumer 3 fits). Don't mind when, the only criteria is SUM(AMOUNT) > 10000 in any 12 consecutive months combination.

But i'm stuck on it. Any ideas?
I'm using SQL Server 2012.
One way to do this is do a select back to the table and sum the rows before the row, and then use that as an inner query to be able to use the sum in the where clause, something like this:

select *
from (
  select
    P1.ID,
    P1.CUSTOMER_ID,
    P1.DATEPAID,
    (select sum(P2.AMOUNT) 
      from PAYMENT P2 
      where P1.CUSTOMER_ID = P2.CUSTOMER_ID and 
      P2.DATEPAID &lt;= P1.DATEPAID and 
      P2.DATEPAID &gt;= dateadd(month, -12, P1.DATEPAID)) as PAID12M
  from
    PAYMENT P1
) X
where PAID12M &gt; 10000


Example in SQL Fiddle.

This will return all the rows where the criteria matches, if you need only customer_id or first row or something like that, you'll have to make the outer query a little bit more complex.