Sql 合并3S\U p的查询
我想展示一下 月/年/总注册/LR注册/BBR注册 我在三个不同的查询中显示结果,但我想在一个查询中编写存储过程…意味着我想在第一个查询中添加第二个和第三个查询Sql 合并3S\U p的查询,sql,sql-server,sql-server-2008,tsql,stored-procedures,Sql,Sql Server,Sql Server 2008,Tsql,Stored Procedures,我想展示一下 月/年/总注册/LR注册/BBR注册 我在三个不同的查询中显示结果,但我想在一个查询中编写存储过程…意味着我想在第一个查询中添加第二个和第三个查询 user\u lastlogin=RegistrationDatetime您正在寻找类似的内容吗 select Month(user_lastlogin) as Month,year(user_lastlogin) as Year, count(*) as 'Total Reg' from bb_user gr
user\u lastlogin=RegistrationDatetime您正在寻找类似的内容吗
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
你的解释很不清楚,但我认为这实际上是你想要的
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
您真的希望所有年份中的当前月份都使用
LR Reg/BBR Reg
吗?而且您的语法与sql server不匹配。我想要的结果是月/Year/Total Reg/LR Reg/BBR Reg-9(month)/2013/1000/300/700…8(month)/2013/2000/600/14000谢谢..将此月(user\u lastlogin)作为月,年(user\u lastlogin)作为年,计数(*)作为'Total Reg',SUM(当user_regtype='LR'然后是1 END时的情况)[LR Reg],SUM(当user_regtype为null或user_regtype='BBR'然后是1 END时的情况)[BBR Reg]按月从bb_用户组(user_lastlogin),按年(user_lastlogin)按月订购desc@KrantiSingh如果这个或其他答案是有用的,请考虑一下。
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