This is my first post, so I hope I've posted this one correctly.

### My problem:

I want to count the number of active customers per day, the last 30 days.

### What I have so far:

In the first column I want to print today, and the last 29 days. This I have done with

```
select distinct trunc(sysdate-dayincrement, 'DD') AS DATES
from   (
  select level as dayincrement
  from   dual
  connect by level <= 30
)

```

I've picked it up here at stackoverflow, and it works perfectly. I can even extend the number of days returned to ex. 365 days. Perfect!

I also have a table that looks like this

```
|Cust# | Start date | End date  |
| 1000 | 01.01.2015 | 31.12.2015|
| 1001 | 02.01.2015 | 31.12.2016|
| 1002 | 02.01.2015 | 31.03.2015|
| 1003 | 03.01.2015 | 31.08.2015|

```

**This is where I feel the problem starts**

I would like to get this result:

```
| Dates    | # of cust |
|04.01.2015|     4     |
|03.01.2015|     4     |
|02.01.2015|     3     |
|01.01.2015|     1     |

```

Here the query would count 1 if:

* Start date <= DATES
* End date >= DATES

Else count 0.

I just don't know how to structure the query.

I tried this, but it didn't work.

```
count(
  IF ENDDATE <= DATES THEN
    IF STARTDATE >= DATES THEN 1 ELSE 0 END IF
  ELSE
    0
  END IF
) AS CUST

```

Any ideas?