如何从更多表中获取总数?

时间:2015-10-10 12:59:13

标签: mysql sql

我想结合三份报告。我想计算每个表(smr_trn_tsalesenquiry,smr_trn_treceivequotation,smr_trn_tsalesorder),三个表的总计数,并使用hrm_mst_temployee中的where条件employee_gid,按上述三个表中的所有主键分组。

select count(*), 
(
   select count(x.enquiry_gid) 
   from smr_trn_tsalesenquiry x 
   where x.created_by=a.employee_gid
) as enquiry_count, 
(
   select count(y.quotation_gid) 
   from smr_trn_treceivequotation y 
   where y.created_by=a.employee_gid
) as quotation_count, 
(
   select count(z.salesorder_gid)
   from smr_trn_tsalesorder z 
   where z.created_by=a.employee_gid and z.salesorder_status not in('SO Amended','Cancelled','Rejected')
) as sales_count 
from hrm_mst_temployee a 
group by a.employee_gid;

1 个答案:

答案 0 :(得分:0)

您可以通过传递employee_id和创建标量函数来完成此操作 从中返回分组计数。

这个link将帮助您了解如何使用函数