create table table1(doc_no varchar(10),date1 datetime,dept_no int)
insert table1
select '1000001','2005-01-02',1 union
select '1000002','2005-01-26',1 union
select '1000003','2005-01-27',1 union
select '1000004','2005-01-28',2
create table table2(theYear Int,theMonth int,begDate datetime,endDate datetime)
Insert table2 Select 2005, 1, '2005-01-01', '2005-01-27'
Union All Select 2005, 2, '2005-01-28', '2005-03-29'
Union All Select 2005, 3, '2005-03-30', '2005-04-28'
Select
doc_no As 部门编号,
(Case theMonth When 1 Then 1 Else 0 End) As 一月份数量,
(Case theMonth When 2 Then 1 Else 0 End) As 二月份数量,
(Case theMonth When 3 Then 1 Else 0 End) As 三月份数量,
(Case theMonth When 4 Then 1 Else 0 End) As 四月份数量,
(Case theMonth When 5 Then 1 Else 0 End) As 五月份数量,
(Case theMonth When 6 Then 1 Else 0 End) As 六月份数量,
(Case theMonth When 7 Then 1 Else 0 End) As 七月份数量,
(Case theMonth When 8 Then 1 Else 0 End) As 八月份数量,
(Case theMonth When 9 Then 1 Else 0 End) As 九月份数量,
(Case theMonth When 10 Then 1 Else 0 End) As 十月份数量,
(Case theMonth When 11 Then 1 Else 0 End) As 十一月份数量,
(Case theMonth When 12 Then 1 Else 0 End) As 十二月份数量
from
(Select A.doc_no,B.theMonth from
table1 A
Inner Join table2 B
On A.date1 Between B.begDate And endDate) T
Group By doc_no,theMonth
create table table1(doc_no varchar(10),date1 datetime,dept_no int)
insert table1
select '1000001','2005-01-02',1 union
select '1000002','2005-01-26',1 union
select '1000003','2005-01-27',1 union
select '1000004','2005-01-28',2
create table table2(theMonth int,begDate datetime,endDate datetime)
insert table2
select 1,'2005-01-1','2005-1-26' union
select 2,'2005-01-27','2005-02-26' union
select 3,'2005-02-27','2005-03-26' union
select 4,'2005-03-27','2005-04-26' union
select 5,'2005-04-27','2005-05-26' union
select 6,'2005-05-27','2005-06-26' union
select 7,'2005-06-27','2005-07-26' union
select 8,'2005-07-27','2005-08-26' union
select 9,'2005-08-27','2005-09-26' union
select 10,'2005-09-27','2005-10-26' union
select 11,'2005-10-27','2005-11-26' union
select 12,'2005-11-27','2005-12-26'
declare @sql varchar(2000)
declare @month int
set @month=1
set @sql='select dept_no 部门编号'
select @sql=@sql+','+quotename(convert(varchar,theMonth)+'月份数量')
+'=sum(case when theMonth='+convert(varchar,@month)+' and date1 between begDate and endDate then 1 else 0 end)'
,@month=@month+1 from table2
select @sql=@sql+' from table1,table2 group by dept_no'
exec(@sql)
create table table1(doc_no varchar(10),date1 datetime,dept_no int)
insert table1
select '1000001','2005-01-02',1 union
select '1000002','2005-01-26',1 union
select '1000003','2005-01-27',1 union
select '1000004','2005-01-28',2
create table table2(theYear Int,theMonth int,begDate datetime,endDate datetime)
Insert table2 Select 2005, 1, '2005-01-01', '2005-01-27'
Union All Select 2005, 2, '2005-01-28', '2005-03-29'
Union All Select 2005, 3, '2005-03-30', '2005-04-28'
select
dept_no,
[一月份]=(select count(doc_no) from table1 t1, table2 t2
where dept_no=a.dept_no and year(date1)=2005 and month(date1)=1
and year(t1.date1)=t2.theYear and date1 between t2.begDate and t2.endDate
),
[二月份]=(select count(doc_no) from table1 t1, table2 t2
where dept_no=a.dept_no and year(date1)=2005 and month(date1)=2
and year(t1.date1)=t2.theYear and date1 between t2.begDate and t2.endDate
)
from table1 a
group by dept_no
create table table1(doc_no varchar(10),date1 datetime,dept_no int)
insert table1
select '1000001','2005-01-02',1 union
select '1000002','2005-01-26',1 union
select '1000003','2005-01-27',1 union
select '1000004','2005-01-28',2
create table table2(theYear Int,theMonth int,begDate datetime,endDate datetime)
Insert table2 Select 2005, 1, '2005-01-01', '2005-01-27'
Union All Select 2005, 2, '2005-01-28', '2005-03-29'
Union All Select 2005, 3, '2005-03-30', '2005-04-28'
Select
dept_no As 部门编号,
SUM((Case theMonth When 1 Then 1 Else 0 End)) As 一月份数量,
SUM((Case theMonth When 2 Then 1 Else 0 End)) As 二月份数量,
SUM((Case theMonth When 3 Then 1 Else 0 End)) As 三月份数量,
SUM((Case theMonth When 4 Then 1 Else 0 End)) As 四月份数量,
SUM((Case theMonth When 5 Then 1 Else 0 End)) As 五月份数量,
SUM((Case theMonth When 6 Then 1 Else 0 End)) As 六月份数量,
SUM((Case theMonth When 7 Then 1 Else 0 End)) As 七月份数量,
SUM((Case theMonth When 8 Then 1 Else 0 End)) As 八月份数量,
SUM((Case theMonth When 9 Then 1 Else 0 End)) As 九月份数量,
SUM((Case theMonth When 10 Then 1 Else 0 End)) As 十月份数量,
SUM((Case theMonth When 11 Then 1 Else 0 End)) As 十一月份数量,
SUM((Case theMonth When 12 Then 1 Else 0 End)) As 十二月份数量
from
(Select A.dept_no,B.theMonth from
table1 A
Inner Join table2 B
On A.date1 Between B.begDate And endDate) T
Group By dept_no,theMonth