I needs to get count of max active users by date range. Acrive users - it is a maximum number of not removed users. I have UsersHistory Table:
| HistoryID | UserID | IsRemoved | OperationID | ModificationDate |
| ------------ | ------------ | ------------ | ------------ | ------------ |
| 1 | 1 | 0 | 'Add' | 2012-07-24 04:27:48 |
| 2 | 2 | 0 | 'Add' | 2012-07-26 04:18:48 |
| 3 | 3 | 0 | 'Add' | 2012-07-27 04:29:48 |
| 4 | 1 | 0 | 'Update' | 2012-07-28 04:47:48 |
| 5 | 2 | 0 | 'Update' | 2012-07-29 04:01:48 |
| 6 | 1 | 1 | 'Remove' | 2012-08-28 04:34:48 |
| 7 | 2 | 1 | 'Remove' | 2012-08-28 04:18:48 |
| 8 | 3 | 1 | 'Remove' | 2012-08-28 04:29:48 |
| 9 | 4 | 0 | 'Add' | 2012-09-24 04:27:48 |
| 10 | 5 | 0 | 'Add' | 2012-09-26 04:18:48 |
| 11 | 6 | 0 | 'Add' | 2012-09-27 04:29:48 |
| 12 | 7 | 0 | 'Add' | 2012-09-27 04:29:48 |
Expected result: Max active users by this period: 4 (HistoryID: 9, 10, 11, 12)
Update1:
HistoryID UserID IsRemoved OperationID ModificationDate
----------------------------------------------------------------------
1 1 0 'Add' 2012-07-24 04:27:48
2 2 0 'Add' 2012-07-26 04:18:48
3 3 0 'Add' 2012-07-27 04:29:48
4 1 1 'Remove' 2012-07-28 04:47:48
5 2 1 'Remove' 2012-07-28 04:47:48
6 3 1 'Remove' 2012-07-28 04:47:48
Expected result: Maximum of active(not removed) users by this period: 3
2条答案
按热度按时间cgh8pdjw1#
This should work for you. It will count total number of operations
Add
and total operations ofRemove
. After It subract total count ofRemove
from total count ofAdd
.EDIT
Here you go (It should count maximum as you need):
mnowg1ta2#
If I understand you correctly you need to make islands by OperationID and count maximum count of rows in 'Add' islands. If so then: