如何将相关版本组合在一起

时间:2010-03-19 12:28:00

标签: sql oracle

select count(a),b,c
from APPLE
  join MANGO  on (APPLE.link=MANGO.link)
  join ORANGE  on (APPLE.link=ORANGE.link)
 where id='camel' 
group by b,c;

b列给出了像

这样的值
1.0
1.0,R
1.0,B
2.0
2.0,B
2.0,R
3.0,C
3.0,R

有没有办法修改上面的查询,以便所有1.0和1.0,R和1.0,B合并为1.0,而2.0,2.0,B合并为2.0,同样的方式为3.0和4.0?

2 个答案:

答案 0 :(得分:1)

SELECT  COUNT(a), REGEXP_REPLACE(b, '([0-9]*\.[0-9]*)(.*)', '\1') b, c
FROM    apple
JOIN    mango
ON      mango.link = apple.link
JOIN    orange
ON      orange.link = apple.link
WHERE   id = 'camel'
GROUP BY
        REGEXP_REPLACE(b, '([0-9]*\.[0-9]*)(.*)', '\1'), c

答案 1 :(得分:1)

如果您无法访问substr(10g +),则可以使用regexp_replace。如果您只需要从1.0汇总到4.0

SELECT COUNT(a), substr(b, 1, 3), c
  FROM APPLE
  JOIN MANGO ON (APPLE.link = MANGO.link)
  JOIN ORANGE ON (APPLE.link = ORANGE.link)
 WHERE id = 'camel'
 GROUP BY substr(b, 1, 3), c;

如果您预计可以达到10.0,则可以使用instr来获取字符数:

SELECT COUNT(a),
       CASE
          WHEN instr(b, ',') > 0 THEN
           substr(b, 1, instr(b, ',') - 1)
          ELSE
           b
       END, c
  FROM APPLE
  JOIN MANGO ON (APPLE.link = MANGO.link)
  JOIN ORANGE ON (APPLE.link = ORANGE.link)
 WHERE id = 'camel'
 GROUP BY CASE
             WHEN instr(b, ',') > 0 THEN
              substr(b, 1, instr(b, ',') - 1)
             ELSE
              b
          END, c;