求一分组SQL语句。。。高难度啊。。。
数据表结构如下:
表名:Member
字段:
ID
LoginID
RegisterTime
Status 值为1,2可为正式,0,9为不正式
要求查询出如下结果
年/月 会员数 正式 非正式
2007-2 45 40 5
2007-3 50 25 25
2007-4 80 75 5
2007-5 90 20 70
这个,查询语句要怎么写???
我已经思考了好长时间了,没有结果。
------解决方案--------------------修改一下:
select convert(varchar(7),RegisterTime,120) as [年/月],count(1) as 会员数,
sum(case status when 1 then 1 when 2 then 1 end) as 正式,
sum(case status when 0 then 1 when 9 then 1 end) as 非正式
from Member
group by convert(varchar(7),RegisterTime,120)
------解决方案--------------------select [年/月]=RegisterTime,会员数=count(*),
正式=sum(case when Status in (1,2) then 1 else 0 end),
非正式=sum(case when Status in (0,9) then 1 else 0 end)
from Member
group by RegisterTime
------解决方案--------------------select convert(varchar(10),RegisterTime,120) as [年/月],会员数=count(*),
sum(case when Status in (1,2) then 1 else 0 end) as [正式],
sum(case when Status in (0,9) then 1 else 0 end) as [非正式]
from Member
group by convert(varchar(10),RegisterTime,120)
这样就ok了