SQL語句---子查詢、行列互換、if語句
阿新 • • 發佈:2021-01-30
技術標籤:python-MySQL
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;
在這裡插入程式碼片
其中子查詢得到的虛擬表格如下所示
在對得到的虛擬表進行行列互換得到的結果如圖所示: