2015-10-10 19 views
1

我想結合三個報告。我想在每個表中計數(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; 
+0

聽起來不錯。你也嘗試過嗎? –

+0

是的,我試過這個,'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其中y.created_by = a.employee_gid)作爲quotation_count, (從smr_trn_tsalesorder z中選擇count(z.salesorder_gid),其中z.created_by = a.employee_gid和z.salesorder_status 不在('SO已修改','已取消','被拒絕'))as sales_count from hrm_mst_temployee by a.employee_gid;' –

+0

請修改您的問題並顯示樣本數據和期望的結果。特別是,您是否想要爲每位員工或整行獲得一行?而且,你的查詢有什麼問題? –

回答

0

您可以通過傳遞僱員標識創建標量函數做到這一點,從 它返回的分組計數。

link將幫助您瞭解如何與職能的工作