select 日期,count(*) as 总人数,sum(case when 性别='男' then 1 else 0 end)as 男生总数,sum(case when 性别='女' then 1 else 0 end)as 女生总数,
sum(case when 是否团员='是' then 1 else 0 end)as 团员总数,
sum(case when 是否团员='是' and 性别='男' then 1 else 0 end)as 男生团员总数,
sum(case when 是否团员='是' and 性别='女' then 1 else 0 end)as 女生团员总数
from TABLE
group by 日期
sum(case when 是否团员='是' then 1 else 0 end)as 团员总数,
sum(case when 是否团员='是' and 性别='男' then 1 else 0 end)as 男生团员总数,
sum(case when 是否团员='是' and 性别='女' then 1 else 0 end)as 女生团员总数
from TABLE
group by 日期
解决方案 »
- 怎样合并两个查询结果而变成一个结果 ?求助....
- 主表与子表通过外键联系后,子表外键定义了cascade ,请问删除主表的操作需要定义成事务吗?
- ACCESS库里面的查询(视图)怎么转换或者导入到sqlserver??
- 为什么不能正常读取NorthWind?
- 如何用sql 语句取出发布复制中以发布的所有<发布的名称>
- SQL2005中,以“机器名”连接数据库和以“机器名+SQLEXPRESS”连接数据库有什么区别?
- 求一复杂平均数问题
- 我该学什么数据库呢?
- Sql Server6.5中已有数据表的Identity如何加上???
- 请教SQL Server的高手
- to zjcxc(: 邹建 :) 等高手 关于存储过程
- 请问这个SQL语句该怎么写?在线等
sum(case when 是否团员='是' then 1 else 0 end)as 团员总数,
sum(case when 是否团员='是' and 性别='男' then 1 else 0 end)as 男生团员总数,
sum(case when 是否团员='是' and 性别='女' then 1 else 0 end)as 女生团员总数
from TABLE
group by convert(varchar(10),日期,120) 若有其他字段統計照樣用以上方法即可。
,男生总数=sum(case 性别 when '男' then 1 else 0 end)
,女生总数=sum(case 性别 when '女' then 1 else 0 end)
,是团员总数=sum(case 是否团员 when '是' then 1 else 0 end)
,否团员总数=sum(case 是否团员 when '否' then 1 else 0 end)
,男生是团员总数=sum(case when 性别='男' and 是否团员='是' then 1 else 0 end)
,女生是团员总数=sum(case when 性别='男' and 是否团员='是' then 1 else 0 end)
from [table]
group by 日期
表名=case when a.colorder=1 then d.name else '' end,
表说明=case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序号=a.colorder,
字段名=a.name,
标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
))) then '√' else '' end,
类型=b.name,
占用字节数=a.length,
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空=case when a.isnullable=1 then '√'else '' end,
默认值=isnull(e.text,''),
字段说明=isnull(g.[value],'')
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sysproperties g on a.id=g.id and a.colid=g.smallid
left join sysproperties f on d.id=f.id and f.smallid=0
--where d.name='要查询的表' --如果只查询指定表,加上此条件
order by a.id,a.colorder
sum(case when 是否团员='否' then 1 else 0 end)as 不是团员总数,
insert 表 select '2004-01-01','男','是'
union all select '2004-01-01','女','否'
union all select '2004-01-01','女','是'
union all select '2004-01-02','男','否'
union all select '2004-01-02','女','是'
union all select '2004-01-02','男','是'
union all select '2004-01-03','男','否'
union all select '2004-01-03','女','否'
union all select '2004-01-03','男','是'
go--查询,为了格式化日期,加convert处理
select 日期=convert(char(10),日期,120),总人数=count(*)
,男生总数=sum(case 性别 when '男' then 1 else 0 end)
,女生总数=sum(case 性别 when '女' then 1 else 0 end)
,是团员总数=sum(case 是否团员 when '是' then 1 else 0 end)
,否团员总数=sum(case 是否团员 when '否' then 1 else 0 end)
,男生是团员总数=sum(case when 性别='男' and 是否团员='是' then 1 else 0 end)
,女生是团员总数=sum(case when 性别='男' and 是否团员='是' then 1 else 0 end)
from 表
group by convert(char(10),日期,120)
go--删除测试
drop table 表/*--测试结果
日期 总人数 男生总数 女生总数 是团员总数 否团员总数 男生是团员总数 女生是团员总数
---------- ----------- ----------- ----------- ----------- ----------- -----------2004-01-01 3 1 2 2 1 1 1
2004-01-02 3 2 1 2 1 1 1
2004-01-03 3 2 1 1 2 1 1(所影响的行数为 3 行)
--*/