SQL語句 子查詢 行列互換 if語句

2021-10-18 06:43:20 字數 3579 閱讀 9926

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

orderby`

group

`) a

group

by a.user_id,a.project

order

by a.

group

;在這裡插入**片

其中子查詢得到的虛擬**如下所示

SQL(行列互換)

有乙個sql題在面試中出現的概率極高,最近有學生出去面試仍然會遇到這樣的題目,在這裡跟大家分享一下。題目 資料庫中有一張如下所示的表,表名為sales。年 季度銷售量 1991111 1991212 1991313 1991414 1992121 1992222 1992323 1992424 要求...

SQL行列互換

有乙個sql題在面試中出現的概率極高,在這裡跟大家分享一下。題目 資料庫中有一張如下所示的表,表名為sales。年 季度銷售量 1991111 1991212 1991313 1991414 1992121 1992222 1992323 1992424 要求 寫乙個sql語句查詢出如下所示的結果。...

SQL行列互換

題目 資料庫中有一張如下所示的表,表名為sales。年 季度銷售量 1991111 1991212 1991313 1991414 1992121 1992222 1992323 1992424 要求 寫乙個sql語句查詢出如下所示的結果。年 一季度二季度 三季度四季度 1991 1112 1314...