Question: I want to get counts of subscribers who terminate and re-enroll in the same month when the Termination reason is 'xxx' for past 12 months in a row.

![Example](https://i.stack.imgur.com/DI93M.png)

In the above example, subscriber '1245' has been terminated for 'xxx' reason but re-enrolled again in the same month. I want counts of how many times this happens month by month for last n number of months.

I tried below code to get PersonIDs but having trouble getting counts in a month.

```
SELECT DISTINCT PersonID FROM Membership A

INNER JOIN (SELECT StartYrMo FROM Membership) B

ON A.EndYrMo = B.StartYrMo

WHERE A.TermReason = 'xxx'

ORDER BY PersonID

```

**EDIT** There is more to it. I want all the PersonIds who does so for at least 6 months in a row. Meaning: Person 'A' is terminated due to 'xxx' reason in 201901. 'A' is again terminated due to same 'xxx' reason in 201902 through 201908 which is minimum of 6 months. I want Ids of everyone who does this.

Answer: 
You *can* use `reduce()` for this by continually adding the results to a list in the accumulator:

```
import numpy as np
from functools import reduce

matrix = 100*np.random.rand(4,4)
matrix=matrix.astype(int)
sum_test = reduce(lambda a,b : a+[sum(b)], list(matrix), [])
print(sum_test)

```

...but you really shouldn't. One of the major points of Numpy is that you can avoid explicit python loops. Instead you should just use the array's `sum` function. You can pass it an `axis` to tell it to sum rows instead of the whole thing:

```
import numpy as np

matrix = np.random.randint(0, 100, [4,4])
print(matrix)
print(matrix.sum(axis = 1))

```

**Result**

```
[[64 89 97 15]
 [12 47 81 31]
 [52 81 37 78]
 [27 64 79 50]]

[265 171 248 220]

```
sum\_test = reduce((lambda a,b : a+b), list(s\_matrix[0]))

above line is your problem,

you are only giving the first row of your matrix instead of giving the whole matrix