Sql server 有没有更好的方法来编写这个子查询,因为它看起来很大?

Sql server 有没有更好的方法来编写这个子查询,因为它看起来很大?,sql-server,Sql Server,我已经尝试检索依赖于不同条件的记录数。我得到了预期的输出,但对我来说它似乎很大 select count(b.verificationdtl_gid) as received, ( select count(b.verificationdtl_gid) from avs_trn_tverification a inner join avs_trn_tverificationdtl b on a.verification_gid =b.verification_gid inner join ve

我已经尝试检索依赖于不同条件的记录数。我得到了预期的输出,但对我来说它似乎很大

select count(b.verificationdtl_gid) as received,
(
select count(b.verificationdtl_gid) 
from avs_trn_tverification a
inner join avs_trn_tverificationdtl b on a.verification_gid =b.verification_gid
inner join ver_mst_tverifier c on a.verifier_gid = c.verifier_gid
where c.verifier_gid='VFI1601203046' and b.sap_flag<>'Y'
)as Normal,
(
select count(b.verificationdtl_gid)
from avs_trn_tverification a
inner join avs_trn_tverificationdtl b on a.verification_gid =b.verification_gid
inner join ver_mst_tverifier c on a.verifier_gid = c.verifier_gid
where c.verifier_gid='VFI1601203046' and b.verification_status='Reject'
) as reject,
(
select count(b.verificationdtl_gid) 
from avs_trn_tverification a
inner join avs_trn_tverificationdtl b on a.verification_gid =b.verification_gid
inner join ver_mst_tverifier c on a.verifier_gid = c.verifier_gid
where c.verifier_gid='VFI1601203046' and b.verification_status='Decline'
)as decline
from avs_trn_tverification a
inner join avs_trn_tverificationdtl b on a.verification_gid =b.verification_gid
inner join ver_mst_tverifier c on a.verifier_gid = c.verifier_gid
where c.verifier_gid='VFI1601203046'

改为使用条件聚合:

select
    count(b.verificationdtl_gid) as received,
    count(case when b.sap_flag <> 'Y' THEN b.verificationdtl_gid end) as Normal,
    count(case when b.verification_status ='Reject' THEN b.verificationdtl_gid end) as reject,
    count(case when b.verification_status ='Decline' THEN b.verificationdtl_gid end) as reject
from avs_trn_tverification a
inner join avs_trn_tverificationdtl b 
    on a.verification_gid = b.verification_gid
inner join ver_mst_tverifier c 
    on a.verifier_gid = c.verifier_gid
where
    c.verifier_gid = 'VFI1601203046'
选择
收到时的计数(b.verificationdtl_gid),
正常计数(b.sap_标志“Y”然后b.verificationdtl_gid end时的情况),
将(b.verification_status='Reject'然后b.verificationdtl_gid end时的情况)计为拒绝,
计数(当b.verification\u status='Decept'然后b.verificationdtl\u gid end时的情况)为拒绝
来自avs\u trn\u tverification a
内部连接avs\u trn\u tverificationdtl b
在a.verification_gid=b.verification_gid上
内部连接版本验证程序c
关于a.verifier\u gid=c.verifier\u gid
哪里
c、 验证器_gid='VFI1601203046'

改用条件聚合:

select
    count(b.verificationdtl_gid) as received,
    count(case when b.sap_flag <> 'Y' THEN b.verificationdtl_gid end) as Normal,
    count(case when b.verification_status ='Reject' THEN b.verificationdtl_gid end) as reject,
    count(case when b.verification_status ='Decline' THEN b.verificationdtl_gid end) as reject
from avs_trn_tverification a
inner join avs_trn_tverificationdtl b 
    on a.verification_gid = b.verification_gid
inner join ver_mst_tverifier c 
    on a.verifier_gid = c.verifier_gid
where
    c.verifier_gid = 'VFI1601203046'
选择
收到时的计数(b.verificationdtl_gid),
正常计数(b.sap_标志“Y”然后b.verificationdtl_gid end时的情况),
将(b.verification_status='Reject'然后b.verificationdtl_gid end时的情况)计为拒绝,
计数(当b.verification\u status='Decept'然后b.verificationdtl\u gid end时的情况)为拒绝
来自avs\u trn\u tverification a
内部连接avs\u trn\u tverificationdtl b
在a.verification_gid=b.verification_gid上
内部连接版本验证程序c
关于a.verifier\u gid=c.verifier\u gid
哪里
c、 验证器_gid='VFI1601203046'