你如何总结和组合一个字符串

时间:2016-09-08 13:46:53

标签: sql plsql oracle11g

我怎样才能得到:

  pn     | min max
  ------ | ------
  ABC123 | 0,0
  ABC123 | 1,1
  ABC123 | 1,1
  ABC123 | 1,1

要:

  pn     | min max
  ------ | -------
  ABC123 | 3,3
  • Oracle数据库11g
  • PL / SQL 11.2.4.0

这就是我现在所拥有的:

SELECT
   PN_INVENTORY_LEVEL.PN,
   PN_INVENTORY_LEVEL.MAXIMUM_STOCK || ',' || PN_INVENTORY_LEVEL.MINIMUM_STOCK AS MIN_MAX
FROM
   PN_INVENTORY_LEVEL
WHERE ("PN_INVENTORY_LEVEL"."PN" = :PN)

3 个答案:

答案 0 :(得分:5)

尝试使用

 SELECT
               PN_INVENTORY_LEVEL.PN,
               Sum(PN_INVENTORY_LEVEL.MAXIMUM_STOCK) || ',' || sum(PN_INVENTORY_LEVEL.MINIMUM_STOCK) AS MIN_MAX
    FROM
               PN_INVENTORY_LEVEL
    WHERE ("PN_INVENTORY_LEVEL"."PN" = :PN)
group by PN_INVENTORY_LEVEL.PN

答案 1 :(得分:0)

尝试

SELECT pn, SUM(min) as min, SUM(max) as max FROM PN_INVENTORY_LEVEL GROUP BY pn

答案 2 :(得分:0)

如果您将min_max作为字符串添加到一列中,我相信这将是您的答案(将tab替换为 您的表名 ):

WITH
tab1 AS(
SELECT t.pn,
       t.min_max,
       INSTR(t.min_max, ',',1) POS
FROM tab t
),
tab3 AS 
(
SELECT tt.pn,
       SUBSTR(tt.min_max, 1,tt.pos-1) st,
       SUBSTR(tt.min_max,tt.pos+1,LENGTH(tt.min_max)-tt.pos) nd
FROM tab1 tt
)
SELECT ttt.pn,
       SUM(ttt.st) || ',' || SUM(ttt.nd) min_max
FROM tab3 ttt
GROUP BY ttt.pn

它给了我结果:

1   ABC123  3,3