[myTable]有2个日期类型列:startD和endD,以及其他列类型。我想看看2015年的每一天都包含在区间[startD,结束]内统计一年中有多少天属于一个区间
declare @t table (A date, B date)
insert into @t (A,B)
select distinct startD, endD
from myTable
declare @start date = '2015-01-01'
while @start < '2016-01-01'
begin
select
case
when @start >= @t.A and @start <= @t.B
then 1
else 0
end as it
from @t
set @start = DateAdd(day,1,@start)
end
此外,另一个问题:比方说,我想算1
和0
值多少都在365
值。考虑到上面的代码,我该如何做到这一点?
如果我改变t
到@t
我得到Must declare the scalar variable "@t".
你在哪看到'多部分标识符无法绑定'错误? – qxg
我在哪里添加注释行 –
你想实现什么?我有一种感觉,就是打印'0s'和'1s'不是你的目标。 –