0
select Month(user_lastlogin) as Month,year(user_lastlogin) as Year,
count(*) as 'Total             Reg' from bb_user 
group by Month(user_lastlogin),year(user_lastlogin)
order by  Month desc

select count(*) as 'LR Reg' from bb_user 
where  user_regtype ='LR'
group by Month(user_lastlogin)
order by  Month desc

select count(*) as 'BBR Reg' from bb_user 
where  user_regtype is null OR user_regtype = 'BBR'
group by Month(user_lastlogin)
order by  Month desc

我想显示像月/年/总Reg/LR Reg/BBR Reg

我在 3 个不同的查询中显示结果但我想在一个查询中我想编写存储过程....意味着我想在第一个查询中添加第二个和第三个查询。

user_lastlogin = 注册日期时间

4

2 回答 2

0

你在寻找这样的东西吗?

SELECT MONTH(user_lastlogin) AS Month,
       YEAR(user_lastlogin) AS Year,
       COUNT(*) AS 'Total Reg',
       SUM(CASE WHEN user_regtype = 'LR' THEN 1 ELSE 0 END) AS 'LR Reg',
       SUM(CASE WHEN IS NULL OR user_regtype = 'BBR' THEN 1 ELSE 0 END) AS 'BBR Reg'
  FROM bb_user 
GROUP BY MONTH(user_lastlogin), YEAR(user_lastlogin)
ORDER BY Year DESC, Month DESC
于 2013-09-14T07:39:24.547 回答
0

您的解释非常不清楚,但我认为这实际上是您想要的。

select
  Month(user_lastlogin) as Month,
  year(user_lastlogin) as Year,
  count(*) as [Total Reg],
  SUM(CASE WHEN user_regtype ='LR' THEN 1 END) [LR Reg],
  SUM(CASE WHEN user_regtype is null OR user_regtype = 'BBR THEN 1 END) [BBR Reg]
from bb_user 
group by
  Month(user_lastlogin),
  year(user_lastlogin)
order by  Month desc
于 2013-09-14T07:39:40.780 回答