34,594
社区成员
发帖
与我相关
我的任务
分享
declare @table1 table (ID int,CreateDate datetime,DiseaseName varchar(200),mobile bigint,Tel varchar(20),Addr nvarchar(50),Remark varchar(10))
insert into @table1(ID,CreateDate,DiseaseName,mobile,Tel,Addr,Remark)
select 1,'2008-10-11','心脏病',13978945612,'010-8798774','北京','无' union
select 2,'2008-11-11','心脏病',13978945611,'010-8798344','北京','无' union
select 3,'2008-12-11','心肌炎',13978945654,'010-8798744','北京','无' union
select 4,'2008-10-10','心肌炎',13978945677,'010-8794774','北京','无'
select *,总数量=[心脏病]+[心肌炎] from (select Convert(varchar(7),createDate,120) 时间, DiseaseName, count(*) DiseaseName1 from @table1 group by DiseaseName,Convert(varchar(7),createDate,120))m
pivot
(
count(DiseaseName1) for DiseaseName in([心脏病],[心肌炎])
)p
/*
时间 心脏病 心肌炎 总数量
------- ----------- ----------- -----------
2008-10 1 1 2
2008-11 1 0 1
2008-12 0 1 1
(3 行受影响)
*/
declare @tb table(ID int,CreateDate datetime,DiseaseName nvarchar(20),Tel varchar(30),Addr nvarchar(20),Remark nvarchar(20))
insert @tb select 1,'2008-10-11',N'心脏病','13978945612,010-8798774',N'北京',N'无'
insert @tb select 2,'2008-11-11',N'心脏病','13978945611,010-8798344',N'北京',N'无'
insert @tb select 3,'2008-12-11',N'心肌炎','13978945654,010-8798744',N'北京',N'无'
insert @tb select 4,'2008-10-10',N'心肌炎','13978945677,010-8794774',N'北京',N'无'
select convert(varchar(7),CreateDate,120),心脏病=sum(case DiseaseName when N'心脏病' then 1 else 0 end),
心肌炎=sum(case DiseaseName when N'心肌炎' then 1 else 0 end),总数量=count(1) from @tb
group by convert(varchar(7),CreateDate,120)
order by convert(varchar(7),CreateDate,120)
declare @table1 table (ID int,CreateDate datetime,DiseaseName varchar(200),mobile bigint,Tel varchar(20),Addr nvarchar(50),Remark varchar(10))
insert into @table1(ID,CreateDate,DiseaseName,mobile,Tel,Addr,Remark)
select 1,'2008-10-11','心脏病',13978945612,'010-8798774','北京','无' union
select 2,'2008-11-11','心脏病',13978945611,'010-8798344','北京','无' union
select 3,'2008-12-11','心肌炎',13978945654,'010-8798744','北京','无' union
select 4,'2008-10-10','心肌炎',13978945677,'010-8794774','北京','无'
select createdate,isnull(心脏病,0) as 心脏病,isnull(心肌炎,0) as 心肌炎,isnull(总数量,0) as 总数量 from
(
select convert(varchar(7),createdate,120) as createdate,isnull(DiseaseName,'总数量') as DiseaseName,count(*) as cnt from @table1 group by convert(varchar(7),createdate,120),DiseaseName with rollup
) as tmp
pivot
(
sum(cnt)
for DiseaseName in (心脏病,心肌炎,总数量)
) as pvt
where createdate is not null
CREATE TABLE T(ID int,CreateDate datetime,DiseaseName nvarchar(20),Tel varchar(30),Addr nvarchar(20),Remark nvarchar(20))
insert T
SELECT 1,'2008-10-11',N'心脏病','13978945612,010-8798774',N'北京',N'无' UNION ALL
SELECT 2,'2008-11-11',N'心脏病','13978945611,010-8798344',N'北京',N'无' UNION ALL
SELECT 3,'2008-12-11',N'心肌炎','13978945654,010-8798744',N'北京',N'无' UNION ALL
SELECT 4,'2008-10-10',N'心肌炎','13978945677,010-8794774',N'北京',N'无'
declare @s nvarchar(4000),@s1 nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([DiseaseName]),@s1=isnull(@s1+',isnull(','isnull(')+quotename([DiseaseName])+',0) '+quotename([DiseaseName])
from T
group by DiseaseName
exec('
select [时间],'+@s1+',[总计]
from
(
select [时间]=convert(varchar(7),CreateDate,120),DiseaseName,[Total]=count(1)
,[总计]=count(1) over(partition by convert(varchar(7),CreateDate,120) )
from T GROUP BY convert(varchar(7),CreateDate,120),DiseaseName
) a
pivot (max([Total]) for DiseaseName in('+@s+'))b
order by [时间]')
时间 心肌炎 心脏病 总计
------- ----------- ----------- -----------
2008-10 1 1 2
2008-11 0 1 1
2008-12 1 0 1
(3 行受影响)
/*----------------
病名 2008年十月 200年十一月 2008年十二月
心肌炎 1 0 1
心脏病 1 1 0
总数量 2 1 1
------------*/
create table table1(ID int,createdate datetime,DiseaseName varchar(10))
insert into table1
select 1,'2008-10-11','心脏病' union all
select 2,'2008-11-11','心脏病' union all
select 3,'2008-12-11','心肌炎' union all
select 1,'2008-10-10','心肌炎'
select isnull(diseasename,'总数量') as 病名,
SUM(case convert(varchar(7),createdate,120) when '2008-10' then 1 else 0 end) as [2008年十月],
SUM(case convert(varchar(7),createdate,120) when '2008-11' then 1 else 0 end) as [2008年十一月],
SUM(case convert(varchar(7),createdate,120) when '2008-12' then 1 else 0 end) as [2008年十二月]
from table1
group by diseasename with rollup
/*--------------
心肌炎 1 0 1
心脏病 1 1 0
总数量 2 1 1
--------------*/
DECLARE @T2 TABLE(ID int,CreateDate nvarchar(10),DiseaseName nvarchar(20))
insert @T2
SELECT 1 , '2008-10-11' , '心脏病' UNION ALL
SELECT 2 , '2008-11-11' , '心脏病' UNION ALL
SELECT 3 , '2008-12-11' , '心肌炎' UNION ALL
SELECT 4 , '2008-10-11' , '心肌炎'
select T 时间,心脏病=isnull(心脏病,0),心肌炎=isnull(心肌炎,0),[总计]=isnull(心脏病,0)+isnull(心肌炎,0)
from
(
SELECT T=convert(varchar(7),CreateDate,120),DiseaseName,[Total]=count(1)
FROM @T2
GROUP BY convert(varchar(7),CreateDate,120),DiseaseName
) A
pivot
(
sum(Total)
for DiseaseName
in([心脏病],[心肌炎])
) pvt
时间 心脏病 心肌炎 总计
------- ----------- ----------- -----------
2008-10 1 1 2
2008-11 1 0 1
2008-12 0 1 1
(3 行受影响)
declare @tb table(ID int,CreateDate datetime,DiseaseName nvarchar(20),Tel varchar(30),Addr nvarchar(20),Remark nvarchar(20))
insert @tb select 1,'2008-10-11',N'心脏病','13978945612,010-8798774',N'北京',N'无'
insert @tb select 2,'2008-11-11',N'心脏病','13978945611,010-8798344',N'北京',N'无'
insert @tb select 3,'2008-12-11',N'心肌炎','13978945654,010-8798744',N'北京',N'无'
insert @tb select 4,'2008-10-10',N'心肌炎','13978945677,010-8794774',N'北京',N'无'
select convert(varchar(7),CreateDate,120),心脏病=sum(case DiseaseName when N'心脏病' then 1 else 0 end),
心肌炎=sum(case DiseaseName when N'心肌炎' then 1 else 0 end),总数量=count(1) from @tb
group by convert(varchar(7),CreateDate,120)
order by convert(varchar(7),CreateDate,120)
/*
心脏病 心肌炎 总数量
------- ----------- ----------- -----------
2008-10 1 1 2
2008-11 1 0 1
2008-12 0 1 1
(所影响的行数为 3 行)
*/
select (DATEPART(YEAR,CreateDate)+'年'+DATEPART(mm,CreateDate)) as 时间,
(select COUNT(diseasename) from table1 where diseasename='心脏病') as 心脏病,
(select COUNT(diseasename) from table1 where diseasename='心肌炎') as 心肌炎,
总数量=SUM(心肌炎+心脏病)
from table1
group by CONVERT(varchar(7),createdate)
--行列互转
/******************************************************************************************************************************************************
以学生成绩为例子,比较形象易懂
整理人:中国风(Roy)
日期:2008.06.06
******************************************************************************************************************************************************/
--1、行互列
--> --> (Roy)生成測試數據
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[Course] nvarchar(2),[Score] int)
Insert Class
select N'张三',N'语文',78 union all
select N'张三',N'数学',87 union all
select N'张三',N'英语',82 union all
select N'张三',N'物理',90 union all
select N'李四',N'语文',65 union all
select N'李四',N'数学',77 union all
select N'李四',N'英语',65 union all
select N'李四',N'物理',85
Go
--2000方法:
动态:
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+' from Class group by [Student]')
生成静态:
select
[Student],
[数学]=max(case when [Course]='数学' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
[语文]=max(case when [Course]='语文' then [Score] else 0 end)
from
Class
group by [Student]
GO
动态:
declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course]
exec('select * from Class pivot (max([Score]) for [Course] in('+@s+'))b')
生成静态:
select *
from
Class
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b
生成格式:
/*
Student 数学 物理 英语 语文
------- ----------- ----------- ----------- -----------
李四 77 85 65 65
张三 87 90 82 78
(2 行受影响)
*/
------------------------------------------------------------------------------------------
go
--加上总成绩(学科平均分)
--2000方法:
动态:
declare @s nvarchar(4000)
set @s=''
Select @s=@s+','+quotename([Course])+'=max(case when [Course]='+quotename([Course],'''')+' then [Score] else 0 end)'
from Class group by[Course]
exec('select [Student]'+@s+',[总成绩]=sum([Score]) from Class group by [Student]')--加多一列(学科平均分用avg([Score]))
生成动态:
select
[Student],
[数学]=max(case when [Course]='数学' then [Score] else 0 end),
[物理]=max(case when [Course]='物理' then [Score] else 0 end),
[英语]=max(case when [Course]='英语' then [Score] else 0 end),
[语文]=max(case when [Course]='语文' then [Score] else 0 end),
[总成绩]=sum([Score]) --加多一列(学科平均分用avg([Score]))
from
Class
group by [Student]
go
--2005方法:
动态:
declare @s nvarchar(4000)
Select @s=isnull(@s+',','')+quotename([Course]) from Class group by[Course] --isnull(@s+',','') 去掉字符串@s中第一个逗号
exec('select [Student],'+@s+',[总成绩] from (select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a
pivot (max([Score]) for [Course] in('+@s+'))b ')
生成静态:
select
[Student],[数学],[物理],[英语],[语文],[总成绩]
from
(select *,[总成绩]=sum([Score])over(partition by [Student]) from Class) a --平均分时用avg([Score])
pivot
(max([Score]) for [Course] in([数学],[物理],[英语],[语文]))b
生成格式:
/*
Student 数学 物理 英语 语文 总成绩
------- ----------- ----------- ----------- ----------- -----------
李四 77 85 65 65 292
张三 87 90 82 78 337
(2 行受影响)
*/
go
--2、列转行
--> --> (Roy)生成測試數據
if not object_id('Class') is null
drop table Class
Go
Create table Class([Student] nvarchar(2),[数学] int,[物理] int,[英语] int,[语文] int)
Insert Class
select N'李四',77,85,65,65 union all
select N'张三',87,90,82,78
Go
--2000:
动态:
declare @s nvarchar(4000)
select @s=isnull(@s+' union all ','')+'select [Student],[Course]='+quotename(Name,'''')--isnull(@s+' union all ','') 去掉字符串@s中第一个union all
+',[Score]='+quotename(Name)+' from Class'
from syscolumns where ID=object_id('Class') and Name not in('Student')--排除不转换的列
order by Colid
exec('select * from ('+@s+')t order by [Student],[Course]')--增加一个排序
生成静态:
select *
from (select [Student],[Course]='数学',[Score]=[数学] from Class union all
select [Student],[Course]='物理',[Score]=[物理] from Class union all
select [Student],[Course]='英语',[Score]=[英语] from Class union all
select [Student],[Course]='语文',[Score]=[语文] from Class)t
order by [Student],[Course]
go
--2005:
动态:
declare @s nvarchar(4000)
select @s=isnull(@s+',','')+quotename(Name)
from syscolumns where ID=object_id('Class') and Name not in('Student')
order by Colid
exec('select Student,[Course],[Score] from Class unpivot ([Score] for [Course] in('+@s+'))b')
go
select
Student,[Course],[Score]
from
Class
unpivot
([Score] for [Course] in([数学],[物理],[英语],[语文]))b
生成格式:
/*
Student Course Score
------- ------- -----------
李四 数学 77
李四 物理 85
李四 英语 65
李四 语文 65
张三 数学 87
张三 物理 90
张三 英语 82
张三 语文 78
(8 行受影响)
*/