2014-11-06 124 views
0

如何將我的結果總計爲一條記錄而不是item_id的多個實例。相反,我需要爲每件商品開具總數量的發票。如何總計SQL結果

查詢結果 http://i.imgur.com/uakv7e5.jpg

select 
inv_mast.default_product_group, 
inv_mast.item_id, 
inv_mast.item_desc,oe_hdr.order_date, 
oe_line.qty_invoiced, 
oe_line.extended_price 

from 
job_price_hdr 

join oe_hdr on oe_hdr.job_price_hdr_uid = job_price_hdr.job_price_hdr_uid 
join oe_line on oe_line.order_no = oe_hdr.order_no 
join inv_mast on inv_mast.inv_mast_uid = oe_line.inv_mast_uid 

where 
oe_line.qty_invoiced> 0 
and 
oe_hdr.customer_id = 100080 
and 
default_product_group = 'FAST' 

order by 
inv_mast.item_id 
+0

SQL有一個sum()函數。你知道如何使用它嗎? – 2014-11-06 17:08:07

+0

只是學習它。 – eggwhites 2014-11-06 17:12:25

回答

1

試試這個,讓我知道如果你需要更多的信息

select 
    inv_mast.item_id,SUM(oe_line.qty_invoiced) 
    from 
    job_price_hdr 

    join oe_hdr on oe_hdr.job_price_hdr_uid = job_price_hdr.job_price_hdr_uid 
    join oe_line on oe_line.order_no = oe_hdr.order_no 
    join inv_mast on inv_mast.inv_mast_uid = oe_line.inv_mast_uid 

    where 
    oe_line.qty_invoiced> 0 
    and 
    oe_hdr.customer_id = 100080 
    and 
    default_product_group = 'FAST' 
    GROUP BY inv_mast.item_id 
    order by 
    inv_mast.item_id 
+0

謝謝Leandro .... – 2014-11-06 17:11:08

+0

當我將列添加回select部分時,它會出錯,因爲選擇列表中的這些列不在Agg函數或group by子句中。當我將它們添加到組中時,它會再次返回每條記錄,但是這次總數爲 – eggwhites 2014-11-06 17:19:06

+0

現在工作正常嗎? – 2014-11-06 17:21:24