在这里运行SQL Server 2008R2,我有一个问题,我的逻辑还没有完全开始,大声笑
好吧,这就是我所拥有的:+-----------+------------+------------------+-------+----------+----------+
| 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 |
+-----------+------------+------------------+-------+----------+----------+
这是我需要的地方:
+-----------+------------+------------------+-------+----------+----------+
| 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 |
+-----------+------------+------------------+-------+----------+----------+
我为你们创建了一个SQL
小提琴,我尝试过一个左手连接尝试但是我失败了,因此我在这里问:
http://sqlfiddle.com/#!3/043de/7
问题:
我有两个表:1包含一个表,其中包含每个时段的行,另一个表只包含"错过的日期"
我需要根据开始和结束[日期]计算错过天数并在上表中保持运行计数
在我的示例中,我的第一个表包含2014-01-14到2014-01-22每天的行数,另一个表格在2014-01-20错过了一天
所以,我需要rank()
或dense_rank()
或任何其他变体才能获得1,2,3计数......
请注意,在这种情况下我不能使用CTE,因为我的全表超过800,000行,因此不会有效IMO
希望有人能协助
谢谢:)
编辑: 我忘了提到重复是没有必要的,它只是我想到计数1,2,3与rank()的唯一方法是让值一直出现以便做一个&#34 ;"分区在rank()函数内部
我已经更新了上面的表格,有没有办法计算错过的天数,直到您能想到的结束日期为止?
答案 0 :(得分:0)
我取消了左连接并用工会取而代之。
select
[dailydays]
,null as [missed_day]
,null as [missed_day_count]
,[count]
,[start]
,[end]
from tbl_1
where dailydays < (select min(missed_day) from tbl_2)
union
select
t1.[dailydays]
,t2.[missed_day]
,(t1.[count] - (select min([count]) from tbl_1 where [dailydays] = (select min([missed_day]) from tbl_2)) + 1)
,t1.[count]
,t1.[start]
,t1.[end]
from tbl_1 t1
left join tbl_2 t2 on t1.[dailydays] = t2.[missed_day]
where t1.dailydays >= (select min(missed_day) from tbl_2)
order by [dailydays]
修改强> 或者是一个复杂的案例陈述:
select
t1.[dailydays]
,t2.[missed_day]
, case when (t1.[count] - (select min([count]) from tbl_1 where [dailydays] = (select min([missed_day]) from tbl_2)) + 1) < 0 then 0 else
(t1.[count] - (select min([count]) from tbl_1 where [dailydays] = (select min([missed_day]) from tbl_2)) + 1) end as [missed_day_count]
,t1.[count]
,t1.[start]
,t1.[end]
from tbl_1 t1
left join tbl_2 t2 on t1.[dailydays] = t2.[missed_day]
order by [dailydays]