合并一些行,并在oracle sql中求和

问题描述 投票:0回答:1

我正在做一些数据操作并有一张桌子:

“源表”

Customer  Account   AMT   Charge_1  Charge_2 Charge_3
AB           E      C      a            g      m
AB           E      C      b            h      n
AB           E      C      c            i      o
AB           B      W      d            j      p
AB           B      W      e            k      q
AB           R      V      f            l      r

我需要具有和输出看起来像:

Customer  Account   AMT   Charge_1  Charge_2  Charge_3
AB           E       C     a+b+c     g+h+i     m+n+o
AB           B       W       d+e       j+k       p+q
AB           R       V         f         l         r

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9kU0pKSi5wbmcifQ==” alt =“在此处输入图像描述”>

请提出我可以用来合并和总结这些内容的内容。感谢您的投入!

谢谢

sql string oracle group-by sum
1个回答
0
投票

您可以使用聚合。值是字符串,因此您可能希望将它们连接起来而不是求和(在字符串上下文中这没有意义):

select
    customer,
    account
    listagg(charge_1, '+') charge_1,
    listagg(charge_2, '+') charge_2,
    listagg(charge_3, '+') charge_3
from mytable
group by customer, account

您可以使用within group子句控制连接字符串中结果的顺序:

select
    customer,
    account
    listagg(charge_1, '+') within group(order by charge_1) charge_1,
    listagg(charge_2, '+') within group(order by charge_2) charge_2,
    listagg(charge_3, '+') within group(order by charge_3) charge_3
from mytable
group by customer, account
© www.soinside.com 2019 - 2024. All rights reserved.