logo  

Java编程实用经验

Java编程实用经验
作者: 陈安廉

摘要:软件开发进阶系列


sql 列合并查询


2023-02-17 10:43:34

-- 列合并

select

qa.id,

qa.qw_name ,

t2.qs,

t2.qcs,

t2.qzs,

t2.qzcs

from

(

select

t.account_id, max(t.qs) as qs , max(t.qcs) as qcs , max(t.qzs) as qzs, max(t.qzcs)as qzcs

from

(

select

t_qs.account_id as account_id, t_qs.qs as qs, t_qcs.qcs as qcs, t_qzs.qs as qzs, t_qzcs.qcs as qzcs

from

( (

select

account_id, count(*) as qs

from

qw_group_account qga

where

is_delete = 0

and tenancy_id = 107668880110011

group by

account_id) as t_qs

join (

select

qga.account_id as account_id, count(qgm.qw_id) as qcs

from

qw_group_member qgm

inner join qw_group_account qga on

qgm.group_id = qga.qw_group_id

and qga.is_delete = 0

where

qgm.is_delete = 0

and qga.is_delete = 0

and qga.tenancy_id = 107668880110011

group by

qga.account_id ) as t_qcs on

t_qcs.account_id = t_qs.account_id

left join (

select

account_id, count(*) as qs

from

qw_group_account qga

where

is_delete = 0

and is_manager = 1

and tenancy_id = 107668880110011

group by

account_id) as t_qzs on

t_qzs.account_id = t_qs.account_id

left join (

select

qga.account_id as account_id, count(qgm.qw_id) as qcs

from

qw_group_member qgm

inner join qw_group_account qga on

qgm.group_id = qga.qw_group_id

where

qgm.is_delete = 0

and qga.is_manager = 1

and qga.is_delete = 0

and qga.tenancy_id = 107668880110011

group by

qga.account_id ) as t_qzcs on

-- t_qzs.account_id = t_qs.account_id

 t_qzcs.account_id = t_qs.account_id ) )as t

group by

t.account_id ) as t2

inner join qw_account as qa on

qa.id = t2.account_id