SQL语句---子查询、行列互换、if语句

该博客详细解析了一项关于项目活动中各状态(推送、创建、删除等)的统计分析,涵盖了用户在不同项目中的参与情况。通过虚拟表和行列转换,揭示了用户在各个项目中的活跃度和贡献度,有助于理解团队协作模式和项目动态。
摘要由CSDN通过智能技术生成

select A.date_range,`USER`,A.USER_ID,A.PROJECT,`GROUP`,
sum(IF(A.staus='pushed to',A.ct, 0)) as `pushed to`,
sum(IF(A.staus='pushed new',A.ct,0)) as `pushed new`,
sum(IF(A.staus='created',A.ct,0)) as `created`,
sum(IF(A.staus='deleted',A.ct,0)) as `deleted`,
sum(IF(A.staus='joined',A.ct,0)) as `joined`,sum(A.ct) as total
from (
	select e.date_range,e.author_name `USER`,e.author_username `USER_ID`,p.name `PROJECT`,p.namespace_name `GROUP`,
	if(action_name='pushed to','pushed to','false') as staus,count(*)  ct
	from events_info e ,projects_info_02 p
	where e.project_id=p.id and action_name='pushed to' 
	group by project_id,author_id
	union
	select  e.date_range,e.author_name `USER`,e.author_username `USER_ID`,p.name `PROJECT`,p.namespace_name `GROUP`,
	if(action_name='pushed new','pushed new','false') as staus,count(*) ct
	from events_info e ,projects_info_02 p
	where e.project_id=p.id and action_name='pushed new'
	group by project_id,author_id
	union
	select  e.date_range,e.author_name `USER`,e.author_username `USER_ID`,p.name `PROJECT`,p.namespace_name `GROUP`,
	if(action_name='created','created','false') as staus,count(*) ct
	from events_info e ,projects_info_02 p
	where e.project_id=p.id and action_name='created'
	group by project_id,author_id
	union
	select  e.date_range,e.author_name `USER`,e.author_username `USER_ID`,p.name `PROJECT`,p.namespace_name `GROUP`,
	if(action_name='deleted','deleted','false') as staus,count(*) ct
	from events_info e ,projects_info_02 p
	where e.project_id=p.id and action_name='deleted'
	group by project_id,author_id
	union
	select  e.date_range,e.author_name `USER`,e.author_username `USER_ID`,p.name `PROJECT`,p.namespace_name `GROUP`,
	if(action_name='left','left','false') as staus,count(*) ct
	from events_info e ,projects_info_02 p
	where e.project_id=p.id and action_name='left'
	group by project_id,author_id
	union
	select  e.date_range,e.author_name `USER`,e.author_username `USER_ID`,p.name `PROJECT`,p.namespace_name `GROUP`,
	if(action_name='joined','joined','false') as staus,count(*) ct
	from events_info e ,projects_info_02 p
	where e.project_id=p.id and action_name='joined'
	group by project_id,author_id
	order by `GROUP`
    ) A
group by A.USER_ID,A.PROJECT  
order by A.GROUP;
在这里插入代码片

其中子查询得到的虚拟表格如下所示
在这里插入图片描述在对得到的虚拟表进行行列互换得到的结果如图所示:

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值