10

我有一张这样的桌子,(有总和客户,每个人都有一些付款):

customerID      Payments   InvoicCode
1                 1000         112
1                 250          456
2                 100          342
1                 20           232
2                 500          654
3                 300          230

我想要的是如下(每行客户付款的总和):

customerID      Payments   InvoicCode   SumPayment
1                 1000         112        1270
1                 250          456        1270
2                 100          342        600
1                 20           232        1270
2                 500          654        600
3                 300          230        300
4

10 回答 10

5

在多行中复制范围更广的数据不是正确的正常形式。考虑更新付款或添加新客户付款的影响 - 您将必须更新该客户的所有相关总数。

创建一个视图/存储过程会更简单,它可以在运行时为您提供总计,您可以在需要时调用它们:

create view CustomerTotals as

  select customerID
        ,sum(Payments) as SumPayment
    from mytable
group by customerID

然后您将使用以下select * from CustomerTotals输出引用它:

customerID      SumPayment
1               1270
2               600
3               300
于 2012-08-27T14:15:33.333 回答
5

这里是:

SELECT t.customerID,
       t.Payments,
       t.InvoicCode,
       aux.SumPayment
FROM tablename t
INNER JOIN
(SELECT customerID,
        SUM(Payments) as SumPayment
 FROM tablename
 GROUP BY customerID) aux ON t.customerID = aux.customerID
于 2012-08-27T14:18:05.153 回答
4

试试这个,(主要适用于任何 rdbms

SELECT  a.*, b. totalPayment
FROM    paymentsTable a
            INNER JOIN
            (
                SELECT customerID, SUM(Payments) totalPayment
                FROM paymentsTable
                GROUP BY customerID
            ) b ON a.customerID = b.customerID

SQLFiddle 演示

于 2012-08-27T14:19:57.707 回答
3

您可以创建一个视图或尝试这样的选择:

SELECT customerID, 
Payments, 
InvoicCode, 
(SELECT SUM(Payments) 
    FROM Customer IC 
    WHERE IC.customerID = OC.customerID )
FROM Customer  OC
于 2012-08-27T14:16:38.793 回答
3

将表加入到自身的求和版本中:

select mytable.customerID, Payments, InvoicCode, SumPayment
from mytable
join (select customerID, sum(Payments) as SumPayment from mytable group by 1) x
  on x.customerID = mytable.customerID
于 2012-08-27T14:17:59.153 回答
3

您可以使用子查询来获取总和,然后将其连接到您的表中以添加其他列。

SELECT x2.customerID
  , x2.payments
  , x2.invoice
  , x1.sumpayment
FROM
(
  select customerID
   ,sum(Payments) as SumPayment
  from yourtable
  group by customerID
) x1
inner join yourtable x2
  ON x1.customerID = x2.customerid

请参阅带有演示的 SQL Fiddle

于 2012-08-27T14:18:05.967 回答
3
select t1.*,sumPay
from table t1,
(select customerID,sum(Payments) as sumPay
 from table
 group by customerID) t2
where t1.cutomerID=t2.customerID
于 2012-08-27T14:16:28.243 回答
2

对于 MSSQL

SELECT Т1.*, Т2.SumPayment
FROM TableName T1 INNER JOIN 
 ( SELECT customerId, SUM(Payments) SumPayment 
    FROM TableName
    GROUP BY customerID  
) T2 ON T1.customerID = T2.customerId
于 2012-08-27T14:15:49.127 回答
2

假设您的 dbms 是 MS Sql-Server,您可以使用SUM(Payments)withOVER子句:

SELECT customerID, Payments,InvoicCode
   ,SumPayment=SUM(Payments)OVER(PARTITION BY customerID)
FROM t

SQL小提琴:http ://sqlfiddle.com/#!3/2ac38/2/0

于 2012-08-27T14:19:53.420 回答
2

SqlFiddle

SELECT
       t.customerID     as customerID,
       t.Payments       as Payments,
       t.InvoicCode     as InvoicCode,
       total            as SumPayment
FROM
       theTable t, 
       (
          SELECT   customerId,
                   sum(Payments) as total
          FROM     theTable
          GROUP BY customerId
       ) tmp
WHERE  
       tmp.customerId = t.customerId
于 2012-08-27T14:19:56.517 回答