来自不同表

时间:2017-04-27 20:21:48

标签: sql sql-server

下午好,

我目前有查询:

SELECT erp_user.login, 
SUM(invoice_header.invoice_amount) as 'Invoices Billed'
FROM erp_user
LEFT JOIN order_header ON erp_user.erp_user_id = order_header.req_by
LEFT JOIN invoice_instruct_header ON order_header.order_id = invoice_instruct_header.order_id
LEFT JOIN invoice_header ON invoice_instruct_header.instruct_id = invoice_header.instruct_no
WHERE erp_user.supervisor_id IS NOT NULL AND user_id_type = 'I' AND erp_user.company_id IS NOT NULL  AND erp_user.is_active = 1 
GROUP BY erp_user.login

它为我提供了员工在我们系统中的总计费清单,其中员工在工作标题上签署了作业。

我很乐意在这个查询中添加开放PO的总数,所以我添加了:

SELECT erp_user.login, SUM(invoice_header.invoice_amount) as 'Invoices Billed', sum(po_header.po_amount) AS "Open PO's"
FROM erp_user
LEFT JOIN order_header ON erp_user.erp_user_id = order_header.req_by
LEFT JOIN invoice_instruct_header ON order_header.order_id = invoice_instruct_header.order_id
LEFT JOIN invoice_header ON invoice_instruct_header.instruct_id = invoice_header.instruct_no
LEFT JOIN po_header ON order_header.order_id = po_header.order_id
WHERE erp_user.supervisor_id IS NOT NULL AND user_id_type = 'I' AND erp_user.company_id IS NOT NULL  AND erp_user.is_active = 1  AND po_header.status = 1
GROUP BY erp_user.login
ORDER BY "Open PO's"

该查询在我的Open PO专栏中给出了数字,但它们是不正确的,我现在正处于无法弄清楚如何排除故障的地步。

有人可以指出我正确的方向吗?我不介意做这项工作,只需要一个指针。谢谢!

2 个答案:

答案 0 :(得分:0)

请注意左连接和Where子句的组合。

SELECT erp_user.login
, SUM(invoice_header.invoice_amount) as 'Invoices Billed'
, sum(CASE WHEN po_header.status = 1 THEN po_header.po_amount ELSE 0 END) AS "Open PO's"
FROM erp_user
LEFT JOIN order_header ON erp_user.erp_user_id = order_header.req_by
LEFT JOIN invoice_instruct_header ON order_header.order_id = invoice_instruct_header.order_id
LEFT JOIN invoice_header ON invoice_instruct_header.instruct_id = invoice_header.instruct_no
LEFT JOIN po_header ON order_header.order_id = po_header.order_id
WHERE erp_user.supervisor_id IS NOT NULL 
AND user_id_type = 'I' 
AND erp_user.company_id IS NOT NULL  
AND erp_user.is_active = 1 
GROUP BY erp_user.login
ORDER BY "Open PO's";

答案 1 :(得分:0)

如果po_header加入order_header,那么在原始查询中,每个po_header的每个invoice_header都会重复一次(导致计算错误)。< / p>

你可以这样使用outer apply()

select 
    erp_user.login
  , [Invoices Billed] = sum(invoice_header.invoice_amount) 
  , x.[Open POs]
from erp_user
  left join order_header
    on erp_user.erp_user_id = order_header.req_by
  left join invoice_instruct_header
    on order_header.order_id = invoice_instruct_header.order_id
  left join invoice_header
    on invoice_instruct_header.instruct_id = invoice_header.instruct_no
  outer apply (
    select 
        [Open POs] = sum(po_header.po_amount)
    from po_header p
      inner join order_header oh 
        on oh.order_id = p.order_id
    where oh.req_by = erp_user.erp_user_id
  ) x
where erp_user.supervisor_id is not null 
  and erp_user.company_id is not null  
  and erp_user.is_active = 1 
  and user_id_type = 'I' 
group by erp_user.login