select isnull(类别,'总计') as 类别, count(1) 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 '研究生' 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 年龄>=35 then 1 else 0 end) as [35岁以下], sum(case when 年龄 between 36 and 45 then 1 else 0 end) as [36-45岁], sum(case 性别 年龄 between 46 and 55 then 1 else 0 end) as [46-55岁] from 表 group by 类别 with rollup http://topic.csdn.net/u/20081101/13/a1a76062-440b-4173-b9f3-63bff3277e40.html
讲个按月份汇总消费的案例: select CONVERT(varchar(7),date,120), sum(cost) from table1 group by CONVERT(varchar(7),date,120) varchar(7)的意思是取前七位:比如2010-07当然也可以部分汇总,将2009年以前的汇总到一条记录,今年的数据每月一条: select case when date<'2010-01-01' then '09年' else CONVERT(varchar(7),date,120) end as date,sum(cost) group by case when date<'2010-01-01' then '09年' else CONVERT(varchar(7),date,120) end 兄弟 你是不是问题发错地方了 你这纯sql问题
第二句 sql前面在"group by"前面少了个 "from table1" 不好意思 失误
楼上正解我来补充SELECT A,B,C,D,count(xx),count(distinct xx),SUM(xx),SUM(case then xx>0 then xx else 0 end),max(xx),min(xx),avg(xx) FROM T简要说明,还很多。GROUP BY A,B,C,D
isnull(类别,'总计') as 类别,
count(1) 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 '研究生' 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 年龄>=35 then 1 else 0 end) as [35岁以下],
sum(case when 年龄 between 36 and 45 then 1 else 0 end) as [36-45岁],
sum(case 性别 年龄 between 46 and 55 then 1 else 0 end) as [46-55岁]
from
表
group by
类别 with rollup
http://topic.csdn.net/u/20081101/13/a1a76062-440b-4173-b9f3-63bff3277e40.html
select CONVERT(varchar(7),date,120), sum(cost) from table1 group by CONVERT(varchar(7),date,120)
varchar(7)的意思是取前七位:比如2010-07当然也可以部分汇总,将2009年以前的汇总到一条记录,今年的数据每月一条:
select case when date<'2010-01-01' then '09年' else CONVERT(varchar(7),date,120) end as date,sum(cost) group by case when date<'2010-01-01' then '09年' else CONVERT(varchar(7),date,120) end 兄弟 你是不是问题发错地方了 你这纯sql问题
不好意思 失误
楼上正解我来补充SELECT A,B,C,D,count(xx),count(distinct xx),SUM(xx),SUM(case then xx>0 then xx else 0 end),max(xx),min(xx),avg(xx)
FROM T简要说明,还很多。GROUP BY A,B,C,D