日期:2014-05-18 浏览次数:20747 次
--每天,每人录入汇总 GO SELECT CONVERT(varchar(100),acceptDate,23) 录入时间,admiID AS 录入人员,COUNT(IDcardNumber) AS 录入总数 FROM dbo.personinformation GROUP BY admiID,CONVERT(varchar(100),acceptDate,23) order by CONVERT(varchar(100),acceptDate,23) ASC
declare @sql varchar(8000) set @sql = 'select convert(varchar(10),acceptDate,120) ' select @sql = @sql + ' , sum(case admiID when ''' + admiID + ''' then 1 else 0 end) [' + admiID + ']' from (select distinct admiID from tb) as a set @sql = @sql + ' from tb group by convert(varchar(10),acceptDate,120)' exec(@sql)
------解决方案--------------------
create table personinformation(acceptDate datetime,admiID nvarchar(20),IDcardNumber nvarchar(20)) insert into personinformation select '2008-01-27','cq',' 10' insert into personinformation select '2008-02-06','cq',' 70' insert into personinformation select '2011-09-19','yml',' 15' insert into personinformation select '2011-09-19','admin',' 124' insert into personinformation select '2011-09-20','yml',' 26' insert into personinformation select '2011-09-20','admin',' 4' insert into personinformation select '2011-09-20','cq',' 8' insert into personinformation select '2011-09-21','yml',' 159' go --select [acceptDate],[admin],[cq],[yml]from personinformation pivot (sum(1) for [admiID] in([admin],[cq],[yml]))b group by go declare @s nvarchar(4000) select @s=isnull(@s+',','')+'['+ admiID +']' from( select distinct [admiID] from personinformation )t exec('select [acceptDate],'+@s+'from (select acceptdate,admiID,count(*)ct from personinformation group by acceptdate,admiID)t pivot (count([admiID]) for [admiID] in('+@s+'))b') /* acceptDate admin cq yml ----------------------- ----------- ----------- ----------- 2008-01-27 00:00:00.000 0 1 0 2008-02-06 00:00:00.000 0 1 0 2011-09-19 00:00:00.000 1 0 1 2011-09-20 00:00:00.000 1 1 1 2011-09-21 00:00:00.000 0 0 1 (5 行受影响) */ go drop table personinformation