如何在Oracle的联接表中将类似数据组合在一起?

时间:2018-01-07 17:38:32

标签: sql oracle

这是我的疑问:

SELECT invoice.invno, invoice.invdate, invprod.partno, orderprod.orderprice 
invprod.shipqty
FROM sh_invoice invoice INNER JOIN sh_invprod invprod
    ON invoice.invno = invprod.invno
INNER JOIN sh_orderprod orderprod 
    ON orderprod.partno = invprod.partno
ORDER BY invoice.invno

以下是输出的片段:

000299   17-10-15 G61631     998.99          1
000299   17-10-15 G61631     998.99          1
000299   17-10-15 G61631     998.99          1
000299   17-10-15 E42364        180         10
000299   17-10-15 G61631     998.99          1
000299   17-10-15 G61631     998.99          1
000299   17-10-15 G61631     998.99          1

如何将类似的partnumbers(G61631)组合成一个单独的字段,以便总计出货价格和总数?我计划做的是找到每个部分的小计,然后查找每个发票的总数。

感谢。

1 个答案:

答案 0 :(得分:1)

你可以使用sum和group by

  SELECT 
      invoice.invno
      , invoice.invdate
      , invprod.partno
      , orderprod.orderprice 
      , sum(invprod.shipqty)
  FROM sh_invoice invoice INNER JOIN sh_invprod invprod
      ON invoice.invno = invprod.invno
  INNER JOIN sh_orderprod orderprod 
      ON orderprod.partno = invprod.partno

  GROUP BY invoice.invno
      , invoice.invdate
      , invprod.partno
      , orderprod.orderprice 
  ORDER BY invoice.invno