【问题标题】:SQL Oracle, merging two result sets [closed]SQL Oracle,合并两个结果集[关闭]
【发布时间】:2019-09-01 07:25:05
【问题描述】:

我有两张表,没有键可以加入。

第一个结果:

select fruit, sum(sales) from normal
group by fruit

预期答案

 Red Apple        5000

 Yellow Mango     7000

第二个结果:

select fruit, sum(sales) from not_normal
group by fruit

预期答案

 Green Apple   300
 Green Mango   500

现在我希望以这种格式显示最终结果:

我可以提取结果 1 和结果 2,并在 Excel 上进行操作。但我想知道如何在 Oracle SQL 中做到这一点。

【问题讨论】:

  • 所以你是在根据描述中的最后一个词发明一个连接条件?如果您有两种以上的苹果(或任何水果......)怎么办?
  • 我不是在做水果,我在做飞机。但这只是一个例子。
  • 没关系,我的问题依然存在;但如果您的数据和结果至少能代表您正在尝试做的事情,那将会很有帮助。暂时忘记 Oracle 和 SQL(和 Excel),并在您的问题中解释作为人类如何关联这两组数据。如果你不能解释你想要的逻辑和结果,那么我们就没有太大希望解释如何实现它——有人可能会猜到并走运,但不太可能......
  • 水果与飞机?我希望您不负责为波音 737 MAX 8 编写新软件。
  • 它们是来自两个没有键的不同表的结果。通常在 R 中,我可以将结果 1 和结果 2 列绑定并执行算术。在 Excel 中,我可以将粘贴 (result1 , result2) 复制到一张 Excel 表中并执行算术运算。我想知道如何在 Oracle sql 上做到这一点而无需导出数据。

标签: sql oracle datatable


【解决方案1】:

你知道它是怎么回事......垃圾进,垃圾出。

SQL> with
  2  normal (fruit, sales) as
  3    (select 'red apple', 5000 from dual union all
  4     select 'yellow mango', 7000 from dual
  5    ),
  6  not_normal (fruit, sales) as
  7    (select 'green apple', 300 from dual union all
  8     select 'green mango', 500 from dual
  9    )
 10  select n.fruit as fruit,
 11         sum(n.sales) nsales,
 12         nn.fruit as fruit_1,
 13         sum(nn.sales) nnsales,
 14         regexp_substr(n.fruit, '\w+$') as fruits,
 15         sum(n.sales) + sum(nn.sales) sales
 16  from normal n join not_normal nn
 17    on regexp_substr(n.fruit, '\w+$') = regexp_substr(nn.fruit, '\w+$')
 18  group by n.fruit, nn.fruit
 19  /

FRUIT            NSALES FRUIT_1        NNSALES FRUITS                    SALES
------------ ---------- ----------- ---------- -------------------- ----------
red apple          5000 green apple        300 apple                      5300
yellow mango       7000 green mango        500 mango                      7500

SQL>

【讨论】:

  • 时间序列之前的土地上的小脚。谢谢。我只是想要合并表的语法
  • 小脚丫头,“regexp_substr(n.fruit, '\w+$')”在做什么?
  • 它从n.fruit字符串中选择最后一个($)单词(\w+)。
  • 是否可以删除字符而不是单词。例如:Apple2b to Apple
  • 如果需要最后2个字符,使用substr(n.fruit, -2);你不需要正则表达式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多