34,590
社区成员
发帖
与我相关
我的任务
分享
declare @sql varchar(8000)
set @sql='select Event,[Sum]=count(1)'
select @sql=@sql+',['+GoToPlace+']=sum(case GoToPlace when '''+GoToPlace+''' then 1 else 0 end)'
from (select distinct GoToPlace from TableA) t
set @sql=@sql+' from TableA group by Event'
exec(@sql)
use Tempdb
go
--> -->
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([Name] nvarchar(5),[GoToPlace] nvarchar(7),[Event] nvarchar(8),[Date] Datetime)
Insert #T
select N'Herry',N'China',N'Shopping','2009-10-1' union all
select N'John',N'Japan',N'Work','2009-10-3' union all
select N'Jack',N'America',N'Study','2003-12-1' union all
select N'Tom',N'India',N'Work','2003-1-1' union all
select N'Herry',N'America',N'Shopping','2004-10-1' union all
select N'John',N'Japan',N'Study','2005-10-3'
Go
declare @s nvarchar(1000)
set @s='select [Event]'
Select @s=@s+','+quotename([GoToPlace])+'=sum(case when [GoToPlace]='+quotename([GoToPlace],'''')+' then 1 else 0 end)' from #T group by [GoToPlace]
exec(@s+' from #T group by [Event]')
(6 個資料列受到影響)
Event America China India Japan
-------- ----------- ----------- ----------- -----------
Shopping 1 1 0 0
Study 1 0 0 1
Work 0 0 1 1
(3 個資料列受到影響)
declare @table table (name nvarchar(10),gotoplace nvarchar(10),event nvarchar(10),[date] datetime)
insert into @table select 'herry','china','shopping','2009-10-1'
union all select 'john','japan','work','2009-10-3'
union all select 'jack','America','study','2003-12-1'
union all select 'tom','india','work','2003-1-1'
union all select 'herry','america','shopping','2004-10-1'
union all select 'john','japan','study','2005-10-3'
select event,[china]= sum(case when gotoplace='china' then 1 else 0 end),
[japan]= sum(case when gotoplace='japan' then 1 else 0 end),
[America]= sum(case when gotoplace='America' then 1 else 0 end),
[America]=sum(case when gotoplace='herry' then 1 else 0 end)
from @table group by [event]
event china japan America America
---------- ----------- ----------- ----------- -----------
shopping 1 0 1 0
study 0 1 1 0
work 0 1 0 0
(3 行受影响)
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([Name] nvarchar(5),[GoToPlace] nvarchar(7),[Event] nvarchar(8),[Date] Datetime)
Insert #T
select N'Herry',N'China',N'Shopping','2009-10-1' union all
select N'John',N'Japan',N'Work','2009-10-3' union all
select N'Jack',N'America',N'Study','2003-12-1' union all
select N'Tom',N'India',N'Work','2003-1-1' union all
select N'Herry',N'America',N'Shopping','2004-10-1' union all
select N'John',N'Japan',N'Study','2005-10-3'
Go
declare @s nvarchar(max)
set @s = 'select * from (select name,event,gotoplace from #t) a
pivot ( count(name) for gotoplace in( '
select @s = @s + stuff((select ','+gotoplace from (select distinct gotoplace from #t) t for xml path('')),1,1,'')
set @s = @s +' ))b'
exec(@s)
event America China India Japan
-------- ----------- ----------- ----------- -----------
Shopping 1 1 0 0
Study 1 0 0 1
Work 0 0 1 1
(3 row(s) affected)
create table TabelA (Name varchar(5),GoToPlace varchar(7),Event varchar(8),Date datetime)
insert into TabelA
select 'Herry','China','Shopping','2009-10-1' union all
select 'John','Japan','Work','2009-10-3' union all
select 'Jack','America','Study','2003-12-1' union all
select 'Tom','India','Work','2003-1-1' union all
select 'Herry','America','Shopping','2004-10-1' union all
select 'John','Japan','Study','2005-10-3'
select * from TabelA
declare @sql varchar(8000)
set @sql='select Event,[Sum]=count(1)'
select @sql=@sql+',['+GoToPlace+']=sum(case GoToPlace when '''+GoToPlace+''' then 1 else 0 end)'
from (select distinct GoToPlace from TabelA) t
set @sql=@sql+' from TabelA group by Event'
exec(@sql)
/*
Event Sum America China India Japan
-------- ----------- ----------- ----------- ----------- -----------
Shopping 2 1 1 0 0
Study 2 1 0 0 1
Work 2 0 0 1 1
*/