求一条SQL语句,SQLServer2005的?解决就送分。。。

yzty 2009-06-21 02:47:27
表【Table1】的结构如下:
ID,CreateDate,DiseaseName,Tel,Addr,Remark
1,2008-10-11,心脏病,13978945612,010-8798774,北京,无
2,2008-11-11,心脏病,13978945611,010-8798344,北京,无
3,2008-12-11,心肌炎,13978945654,010-8798744,北京,无
4,2008-10-10,心肌炎,13978945677,010-8794774,北京,无

如何写SQL语句,查询出的结果如下:

心脏病 心肌炎 总数量
2008年10月份 1 1 2
2008年11月份 1 0 1
2008年12月份 0 1 1

或则得到这样的结果也行
2008年10月份 2008年11月份 2008年12月份
心脏病 1 1 0
心肌炎 1 0 1
总数量 2 1 1

SQLServer2005的语句如何写?????

我已经知道了在ACCESS中的写法如下:
TRANSFORM count(ID) AS CNT
SELECT format(CreateDate,'yyyy-mm'), count(ID) AS [Total Of ID]
FROM Table1
GROUP BY format(CreateDate,'yyyy-mm')
PIVOT DiseaseName;

如何把ACCESS中的查询语句用SQLserver2005的语句实现呢?
...全文
38 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
yzty 2009-06-22
  • 打赏
  • 举报
回复
ai_li7758521

转置就可以了阿
SQL code
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



结果如下:
CreateDate 心脏病 心肌炎 总数量
2008-10 1 1 2
2008-11 1 0 1
2008-12 0 1 1

基本上能够实现,就是总数量计算的应该是心脏病+心肌炎+..的总数量而不是疾病的类型数量
yzty 2009-06-22
  • 打赏
  • 举报
回复
说明一下,日期是可以选择的,记录有多年的,而且病名也不是固定的。
xiequan2 2009-06-22
  • 打赏
  • 举报
回复
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 行受影响)
*/
  • 打赏
  • 举报
回复
又来了。
Jamin_Liu 2009-06-22
  • 打赏
  • 举报
回复
--測試數據
declare @table1 table(
ID int
,CreateDate datetime
,DiseaseName nvarchar(20)
,Tel nvarchar(50)
,Addr nvarchar(20)
,Remark nvarchar(200)
);
insert into @table1
values
(1,'2008-10-11','心脏病','13978945612,010-8798774','北京','无')
,(2,'2008-11-11','心脏病','13978945611,010-8798344','北京','无')
,(3,'2008-12-11','心肌炎','13978945654,010-8798744','北京','无')
,(4,'2008-10-10','心肌炎','13978945677,010-8794774','北京','无');
--查詢
select *
from (select Month(CreateDate) as [Month],isnull(DiseaseName,'总计') as DiseaseName,count(1) as [count] from @table1 where year(CreateDate)=2008 group by grouping sets((Month(CreateDate)),(Month(CreateDate),DiseaseName))) source
pivot (
sum([count])
for [Month] in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) pvt
yzty 2009-06-22
  • 打赏
  • 举报
回复
等待中。。。
usher_gml 2009-06-21
  • 打赏
  • 举报
回复
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)
swot2ly_100 2009-06-21
  • 打赏
  • 举报
回复
转置就可以了阿
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


结果如下:
CreateDate 心脏病 心肌炎 总数量
2008-10 1 1 2
2008-11 1 0 1
2008-12 0 1 1
深夜情感老师 2009-06-21
  • 打赏
  • 举报
回复
路过,为何全是代码
claro 2009-06-21
  • 打赏
  • 举报
回复
帮顶,
ai_li7758521 2009-06-21
  • 打赏
  • 举报
回复

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 行受影响)
feixianxxx 2009-06-21
  • 打赏
  • 举报
回复
推荐楼主使用
心脏病 心肌炎 总数量
2008年10月份 1 1 2
2008年11月份 1 0 1
2008年12月份 0 1 1
列的数量比较少 比较好处理
feixianxxx 2009-06-21
  • 打赏
  • 举报
回复
/*----------------
病名 2008年十月 200年十一月 2008年十二月
心肌炎 1 0 1
心脏病 1 1 0
总数量 2 1 1

------------*/
feixianxxx 2009-06-21
  • 打赏
  • 举报
回复
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

--------------*/

ai_li7758521 2009-06-21
  • 打赏
  • 举报
回复
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 行受影响)
sdhdy 2009-06-21
  • 打赏
  • 举报
回复

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 行)
*/
feixianxxx 2009-06-21
  • 打赏
  • 举报
回复
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)
SQL77 2009-06-21
  • 打赏
  • 举报
回复
--行列互转
/******************************************************************************************************************************************************
以学生成绩为例子,比较形象易懂

整理人:中国风(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 行受影响)
*/

参考
SQL77 2009-06-21
  • 打赏
  • 举报
回复
行转列的

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧