SQL返回4行时的2行总和

时间:2013-11-13 16:43:36

标签: sql sql-server-2008 sum

我正在尝试在SQL中获取以下数据的完整百分比列。这是我查询的结果。

work_order_no status        orderqty        complete precentcomplete
WO-000076     Approved      20.0000         9        5201725
WO-000076     Approved      20.0000         10       15605175
WO-000078     Approved      12000.0000      200      91258.3333333333
WO-000078     Approved      12000.0000      500      228145.833333333

我需要的是2行的结果。第1行将是WO-00076并且百分比完成。第2行将是WO-000078并且百分比完成。以下是我正在使用的查询。

 Select distinct wo.work_order_no,
                wos.status_description,
                wo.order_qty as [ORDERQTY],
                p.good_tot_diff as [COMPLETE],
                (sum(p.good_tot_diff)/wo.order_qty) * 100 as [PERCENTCOMPLETE]
from wo_master as wo,
     process as p,
     wo_statuses as wos,
     so_children as soc,
     so_sales_orders as sos,
     cs_customers as csc
where wo.work_order_no = p.entry_18_data_txt
and wo.work_order_no = soc.work_order_no
and wo.wo_status_id = wos.wo_status_id
and wo.mfg_building_id = @buildid
and wos.wo_status_id = @statusid
group by wo.work_order_no, wos.status_description, wo.order_qty, p.good_tot_diff

所以我希望结果如下所示

work_order_no status        orderqty        complete precentcomplete
WO-000076     Approved      20.0000         19       0.95
WO-000078     Approved      12000.0000      700      0.005

3 个答案:

答案 0 :(得分:0)

试试这个:

Select wo.work_order_no,
    wos.status_description,
    wo.order_qty as [ORDERQTY],
    SUM(p.good_tot_diff) as [COMPLETE],
    SUM(p.good_tot_diff)/wo.order_qty as [PERCENTCOMPLETE]
from wo_master as wo,
    process as p,
    wo_statuses as wos,
    so_children as soc,
    so_sales_orders as sos,
    cs_customers as csc
where wo.work_order_no = p.entry_18_data_txt
and wo.work_order_no = soc.work_order_no
and wo.wo_status_id = wos.wo_status_id
and wo.mfg_building_id = @buildid
and wos.wo_status_id = @statusid
group by wo.work_order_no, wos.status_description, wo.order_qty

答案 1 :(得分:0)

这应该是你想要的。完整列不应包含在GROUP BY中,并且应该相加。

SELECT DISTINCT wo.work_order_no, wos.status_description, wo.order_qty AS [ORDERQTY], SUM(p.good_tot_diff) AS [COMPLETE], (sum(p.good_tot_diff)/wo.order_qty) * 100 AS [PERCENTCOMPLETE]
FROM wo_master AS wo,
     process AS p,
     wo_statuses AS wos,
     so_children AS soc,
     so_sales_orders AS sos,
     cs_customers AS csc
WHERE wo.work_order_no = p.entry_18_data_txt
  AND wo.work_order_no = soc.work_order_no
  AND wo.wo_status_id = wos.wo_status_id
  AND wo.mfg_building_id = @buildid
  AND wos.wo_status_id = @statusid
GROUP BY wo.work_order_no,
         wos.status_description,
         wo.order_qty;

答案 2 :(得分:0)

SELECT A.*, (COMPLETE/wo.order_qty) * 100 as [PERCENTCOMPLETE]
FROM
(
Select distinct wo.work_order_no,
                wos.status_description,
                wo.order_qty as [ORDERQTY],
                SUM(p.good_tot_diff) as [COMPLETE],
from wo_master as wo,
     process as p,
     wo_statuses as wos,
     so_children as soc,
     so_sales_orders as sos,
     cs_customers as csc
where wo.work_order_no = p.entry_18_data_txt
and wo.work_order_no = soc.work_order_no
and wo.wo_status_id = wos.wo_status_id
and wo.mfg_building_id = @buildid
and wos.wo_status_id = @statusid
group by wo.work_order_no, wos.status_description, wo.order_qty 
) A