CREATE TABLE [test]
([ywSJ] [datetime] NOT NULL ,
[XinXiLY] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL )insert into test select '2012-01-01','学生'
insert into test select '2012-01-01','老师'
insert into test select '2012-01-01','老师'
insert into test select '2012-01-01','学生'
insert into test select '2012-01-01','工人'
insert into test select '2012-01-01','工人'
insert into test select '2012-01-01','学生'
insert into test select '2012-01-01','工人'
insert into test select '2012-01-01','学生'insert into test select '2012-01-02','学生'
insert into test select '2012-01-02','老师'
insert into test select '2012-01-02','老师'
insert into test select '2012-01-02','学生'
insert into test select '2012-01-02','工人'
insert into test select '2012-01-02','工人'
insert into test select '2012-01-03','学生'
insert into test select '2012-01-03','工人'
insert into test select '2012-01-03','学生'
select * from testselect convert(char(10),ywsj,120) as 业务日期,ISNULL(XinXiLY,'') AS 来源途径,count(*) as 人数
from test where convert(char(10),ywsj,120) between '2012-01-01' and '2012-01-03'
GROUP BY ISNULL(XinXiLY,''),convert(char(10),ywsj,120)
我想得到的结果是
业务日期 工人 老师 学生
2011-01-01 3 2 4
2011-01-02 2 2 2
2011-01-03 1 0 2要得到这样的结果 语句该怎么写啊?
([ywSJ] [datetime] NOT NULL ,
[XinXiLY] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL )insert into test select '2012-01-01','学生'
insert into test select '2012-01-01','老师'
insert into test select '2012-01-01','老师'
insert into test select '2012-01-01','学生'
insert into test select '2012-01-01','工人'
insert into test select '2012-01-01','工人'
insert into test select '2012-01-01','学生'
insert into test select '2012-01-01','工人'
insert into test select '2012-01-01','学生'insert into test select '2012-01-02','学生'
insert into test select '2012-01-02','老师'
insert into test select '2012-01-02','老师'
insert into test select '2012-01-02','学生'
insert into test select '2012-01-02','工人'
insert into test select '2012-01-02','工人'
insert into test select '2012-01-03','学生'
insert into test select '2012-01-03','工人'
insert into test select '2012-01-03','学生'
select * from testselect convert(char(10),ywsj,120) as 业务日期,ISNULL(XinXiLY,'') AS 来源途径,count(*) as 人数
from test where convert(char(10),ywsj,120) between '2012-01-01' and '2012-01-03'
GROUP BY ISNULL(XinXiLY,''),convert(char(10),ywsj,120)
我想得到的结果是
业务日期 工人 老师 学生
2011-01-01 3 2 4
2011-01-02 2 2 2
2011-01-03 1 0 2要得到这样的结果 语句该怎么写啊?
select ywsj,sum(case XinXiLY when '学生' then 1 else 0 end) [学生],
sum(...) [],
sum(...) []
from tb
group by ywsj
create TABLE [test]
([ywSJ] [datetime] NOT NULL ,
[XinXiLY] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL )insert into test select '2012-01-01',N'学生'
insert into test select '2012-01-01',N'老师'
insert into test select '2012-01-01',N'老师'
insert into test select '2012-01-01',N'学生'
insert into test select '2012-01-01',N'工人'
insert into test select '2012-01-01',N'工人'
insert into test select '2012-01-01',N'学生'
insert into test select '2012-01-01',N'工人'
insert into test select '2012-01-01',N'学生'insert into test select '2012-01-02',N'学生'
insert into test select '2012-01-02',N'老师'
insert into test select '2012-01-02',N'老师'
insert into test select '2012-01-02',N'学生'
insert into test select '2012-01-02',N'工人'
insert into test select '2012-01-02',N'工人'
insert into test select '2012-01-03',N'学生'
insert into test select '2012-01-03',N'工人'
insert into test select '2012-01-03',N'学生'
select * from
(select * from test) p
pivot
(
count(Xinxily)
for XinxiLy in ([学生],[工人],[老师])
)as pvt/*
ywSJ 学生 工人 老师
2012-01-01 00:00:00.000 4 3 2
2012-01-02 00:00:00.000 2 2 2
2012-01-03 00:00:00.000 2 1 0
*/
[MingCheng] [varchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
) ON [PRIMARY]
GOxinxily字段的值来源于表zd_test 名称
可能会有不少值,按楼上方法写有些不灵活,能否有更好的方法实现呢?谢谢
insert into zd_test select '学生'
insert into zd_test select '工人'
insert into zd_test select '老师'
insert into zd_test select '校长'
CREATE TABLE [test]
([ywSJ] [datetime] NOT NULL ,
[XinXiLY] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL )insert into test select '2012-01-01','学生'
insert into test select '2012-01-01','老师'
insert into test select '2012-01-01','老师'
insert into test select '2012-01-01','学生'
insert into test select '2012-01-01','工人'
insert into test select '2012-01-01','工人'
insert into test select '2012-01-01','学生'
insert into test select '2012-01-01','工人'
insert into test select '2012-01-01','学生'insert into test select '2012-01-02','学生'
insert into test select '2012-01-02','老师'
insert into test select '2012-01-02','老师'
insert into test select '2012-01-02','学生'
insert into test select '2012-01-02','工人'
insert into test select '2012-01-02','工人'
insert into test select '2012-01-03','学生'
insert into test select '2012-01-03','工人'
insert into test select '2012-01-03','学生'
select * from test
--
select convert(char(10),ywsj,120) as 业务日期,ISNULL(XinXiLY,'') AS 来源途径,count(*) as 人数
from test where convert(char(10),ywsj,120) between '2012-01-01' and '2012-01-03'
GROUP BY ISNULL(XinXiLY,''),convert(char(10),ywsj,120)-----------------------------------------------------------------------------------------
declare @str varchar(1000)
set @str=''
select @str=@str+','+[XinXiLY]+
'=sum(case when [XinXiLY]='+QUOTENAME([XinXiLY],'''')+'then 1 else 0 end)'
from [test] group by [XinXiLY]
print @str
set @str='select convert(char(10),ywsj,120) as 业务日期'+@str+' from [test] group by [ywSJ]'
print @str
exec(@str)
-----------------------------------------------------------------------------------------
/*
--我想得到的结果是
业务日期 工人 老师 学生
2012-01-01 3 2 4
2012-01-02 2 2 2
2012-01-03 1 0 2
*/
drop table [test]
(select * from test) p
pivot
(
count(Xinxily)
for XinxiLy in ([学生],[工人],[老师])
)as pvtpivot是什么意思啊,表名?
PIVOT 通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执行聚合。 PIVOT 提供的语法比一系列复杂的 SELECT...CASE 语句中所指定的语法更简单和更具可读性。
但是二楼使用的PIVOT只有2005以后版本的SQLServer才支持。。