如何计算总金额,即使有重复的数字也不分组?

huangapple go评论75阅读模式
英文:

How to sum total amount without grouping even have duplicated number?

问题

  1. 结果
  2. 20.0 VC10001
  3. 10.0 VC10003
  4. ----------------------------

可能显示结果如下吗?

  1. 结果
  2. 10.0 VC10001
  3. 10.0 VC10001
  4. 10.0 VC10003

希望能帮助到您。

英文:
  1. table_a table_b
  2. desc amount ID vcref ID
  3. banana 2.00 101 VC10001 101
  4. apple 3.00 101 VC10001 101
  5. orange 5.00 101 VC10003 101
  6. select sum(a.amount),b.vcref from table_a a,table_b b where a.ID=b.ID group by b.vcref;
  7. result
  8. 20.0 VC10001
  9. 10.0 VC10003
  10. -------------------------------------------

May I know it is possible to show result like this ?

  1. result
  2. 10.0 VC10001
  3. 10.0 VC10001
  4. 10.0 VC10003

Anyone help is much appreciated.

答案1

得分: 1

选择作为内部查询的总和:

  1. select
  2. (select sum(amount) from table_a) as 结果,
  3. vcref
  4. from table_b
英文:

Select the sum as an inner query:

  1. select
  2. (select sum(amount) from table_a) as result,
  3. vcref
  4. from table_b

答案2

得分: 1

使用相关子查询的一种选择是:

示例数据:

  1. SQL> with
  2. 2 table_a (c_desc, amount, id) as
  3. 3 (select 'banana', 2, 101 from dual union all
  4. 4 select 'apple' , 3, 101 from dual union all
  5. 5 select 'orange', 5, 101 from dual
  6. 6 ),
  7. 7 table_b (vcref, id) as
  8. 8 (select 'vc1001', 101 from dual union all
  9. 9 select 'vc1001', 101 from dual union all
  10. 10 select 'vc1003', 101 from dual
  11. 11 )
  12. 查询:
  13. 12 select b.vcref,
  14. 13 (select sum(a.amount) from table_a a where a.id = b.id) total
  15. 14 from table_b b;
  16. VCREF TOTAL
  17. ------ ----------
  18. vc1001 10
  19. vc1001 10
  20. vc1003 10
  21. SQL>;
英文:

One option is to use a correlated subquery:

Sample data:

  1. SQL> with
  2. 2 table_a (c_desc, amount, id) as
  3. 3 (select 'banana', 2, 101 from dual union all
  4. 4 select 'apple' , 3, 101 from dual union all
  5. 5 select 'orange', 5, 101 from dual
  6. 6 ),
  7. 7 table_b (vcref, id) as
  8. 8 (select 'vc1001', 101 from dual union all
  9. 9 select 'vc1001', 101 from dual union all
  10. 10 select 'vc1003', 101 from dual
  11. 11 )

Query:

  1. 12 select b.vcref,
  2. 13 (select sum(a.amount) from table_a a where a.id = b.id) total
  3. 14 from table_b b;
  4. VCREF TOTAL
  5. ------ ----------
  6. vc1001 10
  7. vc1001 10
  8. vc1003 10
  9. SQL>

huangapple
  • 本文由 发表于 2023年2月8日 10:45:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75380938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定