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 日期

解决方案 »

  1.   

    日期可能會帶小時、分等等,若精確到天,可用:select convert(varchar(10),日期,120) as 日期,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 convert(varchar(10),日期,120) 若有其他字段統計照樣用以上方法即可。
      

  2.   

    select 日期,总人数=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 [table]
    group by 日期
      

  3.   

    --或者,在查询分析器中执行下面的代码SELECT 
    表名=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
      

  4.   

    再加上:
    sum(case when 是否团员='否' then 1 else 0 end)as 不是团员总数,
      

  5.   

    --测试--测试数据create table 表(日期 datetime,性别 char(2),是否团员 char(2))
    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 行)
    --*/