日期:2014-05-17 浏览次数:20683 次
if OBJECT_ID('tempdb..#temp', 'u') is not null drop table #temp;
go
create table #temp( [ID] int, [凭据编号] varchar(100), [登记日期] DATETIME, [联系人] varchar(100), [联系电话] varchar(100), [所属部门] varchar(100));
insert #temp
select '1','2013050201','2013-05-02','张三','110','销售部' union all
select '2','2013050202','2013-05-02','李斯','110','销售部' union all
select '3','2013050301','2013-05-03','王五','119','开发部' union all
select '4','2013050401','2013-05-04','赵六',null,'行政部'
--SQL:
DECLARE @department NVARCHAR(100), @month CHAR(6), @sql NVARCHAR(MAX), @collist NVARCHAR(MAX)
SELECT @department = N'销售部', @month = '201305'
SET @collist = ''
SELECT @collist = @collist + QUOTENAME(number+1)+' = SUM(CASE DAY(AllMonthDay) WHEN '+ LTRIM(number+1) +' THEN CNT END),'