Running SQL Server 2008R2 here and I have an issue where my logic isn't quite kicking in yet, lol
ok, this is what I have:
+-----------+------------+------------------+-------+----------+----------+
| DAILYDAYS | MISSED_DAY | MISSED_DAY_COUNT | COUNT | START | END |
+-----------+------------+------------------+-------+----------+----------+
| 20140114 | (null) | (null) | 0 | 20140114 | 20140122 |
| 20140115 | (null) | (null) | 1 | 20140114 | 20140122 |
| 20140116 | (null) | (null) | 2 | 20140114 | 20140122 |
| 20140117 | (null) | (null) | 3 | 20140114 | 20140122 |
| 20140118 | (null) | (null) | 4 | 20140114 | 20140122 |
| 20140119 | (null) | (null) | 5 | 20140114 | 20140122 |
| 20140120 | 20140120 | 1 | 6 | 20140114 | 20140122 |
| 20140121 | (null) | (null) | 7 | 20140114 | 20140122 |
| 20140122 | (null) | (null) | 8 | 20140114 | 20140122 |
+-----------+------------+------------------+-------+----------+----------+
This is where I need to be:
+-----------+------------+------------------+-------+----------+----------+
| DAILYDAYS | MISSED_DAY | MISSED_DAY_COUNT | COUNT | START | END |
+-----------+------------+------------------+-------+----------+----------+
| 20140114 | (null) | (null) | 0 | 20140114 | 20140122 |
| 20140115 | (null) | (null) | 1 | 20140114 | 20140122 |
| 20140116 | (null) | (null) | 2 | 20140114 | 20140122 |
| 20140117 | (null) | (null) | 3 | 20140114 | 20140122 |
| 20140118 | (null) | (null) | 4 | 20140114 | 20140122 |
| 20140119 | (null) | (null) | 5 | 20140114 | 20140122 |
| 20140120 | 20140120 | 1 | 6 | 20140114 | 20140122 |
| 20140121 | (null) | 2 | 7 | 20140114 | 20140122 |
| 20140122 | (null) | 3 | 8 | 20140114 | 20140122 |
+-----------+------------+------------------+-------+----------+----------+
I have created a SQL
Fiddle for you guys with a solution that I have tried with a left join but I failed, hence my asking here :
http://sqlfiddle.com/#!3/043de/7
Problem:
I have two tables: 1 that contains a table with rows for each day of the period and another table that contains only the date of "missed days"
I need to count the number of missed days and keep a running count in the table above based on the start and end [dates]
In my example, my 1st table contains rows for each day from 2014-01-14 through 2014-01-22 and another table has a missed day on 2014-01-20
So, i need to have a rank()
or a dense_rank()
or any other variants in order to get to the 1,2,3 counts...
Please note that I cannot use a CTE in this situation as my full table is over 800,000 lines, so that wouldn't be efficient IMO
Hoping some one can assist
Thanks :)
Edit: i forgot to mention that repeating is not necessary, its just that the only way i had thought of having a count of 1,2,3 with rank() is to have the value appear all the time in order to do a "partition by" inside the rank() function
i've updated the table above, is there a way to count the number of missed days until the end date you can think of ?