表:uaer
 有: ID、年龄、注册日期、等字段 要求按照月、季分组求出,年龄在25岁以下、25-35、35-45、45-60、60岁以上的用户数 求一个完整的算法,不胜感激

解决方案 »

  1.   


    按月计
    SELECT t.month, count(t.age) as noofuser
    (
    SELECT MONTH(regDate) AS [month]
    case when age < 25 then 'ageless25'
    when age >=25 and age<35 then 'age25to35'  
    when age >=35 and age<45 then 'age35to45'  
    when age >=45 and age<60 then 'age45to60' 
    when age >=60 then 'agebigger60'  
     end as age
    ) t
    GROUP BY t.month
      

  2.   

    -- 接楼上的
    -- 楼上的月份 可能最好 按 mm = rtrim(year(regDate)) + rtrim(month(regDate)) 来做,否则就没有年份了
    -- 我来写季度的select 
    季度 = a.qq,
    年龄段 = a.agepart,
    人数 = COUNT(*)
    from (
    select 
    qq = rtrim(year(regDate)) + '年' + rtrim(datepart(quarter, regDate))+'季度', -- 年份季度
    agepart = case when age < 25 then '<25'
    when age >=25 and age<35 then '25-35'   
    when age >=35 and age<45 then '35-45'   
    when age >=45 and age<60 then '45-60'  
    when age >=60 then '>60'
    end
    from uaer
    ) a
    group by a.qq, a.agepart