求一句sql,根据日期区间遍历

will_bi 2011-03-23 01:57:26
A表
ID StartDate EndDate
1 2011-3-10 2011-3-16
B表
ID AID
1 1
2 1

C表
ID BID EffectDate
1 1 2011-3-10
2 1 2011-3-10
3 1 2011-3-10
4 1 2011-3-11
5 2 2011-3-11
6 2 2011-3-16
7 1 2011-3-11
8 1 2011-3-14
9 2 2011-3-11
10 2 2011-3-12
所需查询结果
BID 2011-3-10 2011-3-11 2011-3-12 2011-3-13 2011-3-14 2011-3-15 2011-3-16
1 3 2 0 0 1 0 0
2 0 2 1 0 0 0 1


多谢
...全文
250 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
will_bi 2011-03-24
  • 打赏
  • 举报
回复
谢谢6楼提供思路
chenyunbbs 2011-03-23
  • 打赏
  • 举报
回复
你可以试着用游标去做会好点 我主要是不值到你的业务···
--小F-- 2011-03-23
  • 打赏
  • 举报
回复
select
bid ,
max(case when convert(varchar(10),date,120)='2011-03-10' then [count] end) as [2011-03-10] ,
max(case when convert(varchar(10),date,120)='2011-03-11' then [count] end) as [2011-03-11] ,
max(case when convert(varchar(10),date,120)='2011-03-12' then [count] end) as [2011-03-12] ,
max(case when convert(varchar(10),date,120)='2011-03-13' then [count] end) as [2011-03-13] ,
max(case when convert(varchar(10),date,120)='2011-03-14' then [count] end) as [2011-03-14] ,
max(case when convert(varchar(10),date,120)='2011-03-15' then [count] end) as [2011-03-15] ,
max(case when convert(varchar(10),date,120)='2011-03-16' then [count] end) as [2011-03-16]
from
(select dateadd(dd,number,b.StartDate) as date,c.bid,
(select count(1) from c where bid=c.bid and EffectDate=dateadd(dd,number,a.StartDate)) as [count]
from
master..spt_values t ,b,c
where
type='p'
and
dateadd(dd,number,a.StartDate)<=a.EndDate and a.id=b.aid and b.id=c.bid
group by
dateadd(dd,number,a.StartDate),c.bid) a
group by bid
--小F-- 2011-03-23
  • 打赏
  • 举报
回复
建立一个时间表与你的表 LEFT JOIN
快溜 2011-03-23
  • 打赏
  • 举报
回复
create table tba(id int,StartDate datetime,EndDate datetime)
insert into tba select 1,'2011-3-10','2011-3-16'
create table tbb(id int,aid int)
insert into tbb select 1,1 union select 2,1

create table tbc(id int,bid int,EffectDate datetime)
insert into tbc
select 1,1,'2011-3-10' union all
select 2,1,'2011-3-10' union all
select 3,1,'2011-3-10' union all
select 4,1,'2011-3-11' union all
select 5,2,'2011-3-11' union all
select 6,2,'2011-3-16' union all
select 7,1,'2011-3-11' union all
select 8,1,'2011-3-14' union all
select 9,2,'2011-3-11' union all
select 10,2,'2011-3-12'


declare @str varchar(3000)
set @str=''
select @str= @str+' ,max(case when convert(varchar(10),date,120)='''+
convert(varchar(10),dateadd(dd,number,b.StartDate),120)+''' then [count] end) as ['+
convert(varchar(10),dateadd(dd,number,b.StartDate),120)+']'
from master..spt_values a,tba b
where type='p' and dateadd(dd,number,b.StartDate)<=b.EndDate
set @str='select bid'+@str+' from (select dateadd(dd,number,b.StartDate) as date,d.bid,
[count]=(select count(1) from tbc where bid=d.bid and EffectDate=dateadd(dd,number,b.StartDate))
from master..spt_values a,tba b,tbb c,tbc d
where type=''p'' and dateadd(dd,number,b.StartDate)<=b.EndDate
and b.id=c.aid and c.id=d.bid
group by dateadd(dd,number,b.StartDate),d.bid) a group by bid'
--print @str
exec(@str)
/*
bid 2011-03-10 2011-03-11 2011-03-12 2011-03-13 2011-03-14 2011-03-15 2011-03-16
----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
1 3 2 0 0 1 0 0
2 0 2 1 0 0 0 1
警告: 聚合或其他 SET 操作消除了空值。
bluesmiler 2011-03-23
  • 打赏
  • 举报
回复
把表A弄成如下格式就简单多了,或者建立个日历履历表也可以啊,根据表A求出时间段区间,在行转列
ID Date
1 2011-3-10
2 2011-3-11
3 2011-3-12
4 2011-3-13
5 2011-3-14
6 2011-3-15
6 2011-3-16

qgqch2008 2011-03-23
  • 打赏
  • 举报
回复
就是行轉列啊……
will_bi 2011-03-23
  • 打赏
  • 举报
回复
不只是行转列
查询出来的日期要取A表日期区间内所有日期,可能在C表没出现过
昵称被占用了 2011-03-23
  • 打赏
  • 举报
回复
不就是行列转换吗,论坛中很多答案的
will_bi 2011-03-23
  • 打赏
  • 举报
回复
格式有点乱
简单的说就是取出B表ID在A表的日期区间内所有日期的总数

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧