2012-08-27 17 views
10

Ho una tabella come questa, (c'è clienti somma che ognuno ha alcuni pagamenti):selezionare i dati e somma di una colonna per una colonna distinta nei dati

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

Quello che voglio è come qui di seguito (somma di un pagamento cliente in ogni riga):

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 
+5

quale database? – Bohemian

risposta

5

Non è propriamente la forma normale che i dati con ambito più ampio vengano duplicati in più righe. Pensa all'impatto dell'aggiornamento di un pagamento o all'aggiunta di un nuovo pagamento da parte del cliente: dovrai aggiornare tutti i totali pertinenti per quel cliente.

Sarebbe più semplice per creare una stored procedure vista/che ti dà i totali in fase di esecuzione, che si può chiamare ogni volta che avete bisogno di loro:

create view CustomerTotals as 

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

allora si sarebbe fare riferimento a questo con select * from CustomerTotals con uscita come:

customerID  SumPayment 
1    1270 
2    600 
3    300 
+2

Forse sì, ma tecnicamente questo non risponde alla domanda. Inoltre, non presumere troppo: potrebbe volere che la colonna determini la percentuale di ciascuna riga. – Bohemian

+1

Perché non selezionare semplicemente l'OP richiesto? –

+0

@ Bohemian, aF .: Hai ragione, aspetterò la risposta da OP e rimuovere la mia risposta se è irrilevante.Ho letto la domanda come l'aggiornamento dei totali nella tabella originale come un'altra colonna, che ho riletto non è necessariamente l'intento. Grazie per il tuo input :) – mellamokb

2

Per 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 
+0

non funzionerà affatto – Bohemian

+3

questo funzionerà se l'OP sta usando MySQL, ma se MSSQL allora non lo farà. –

+0

Ho aggiornato anche la mia risposta per MSSQL. Grazie – hgulyan

3
select t1.*,sumPay 
from table t1, 
(select customerID,sum(Payments) as sumPay 
from table 
group by customerID) t2 
where t1.cutomerID=t2.customerID 
3

È possibile creare una vista o provare una selezionata come questo:

SELECT customerID, 
Payments, 
InvoicCode, 
(SELECT SUM(Payments) 
    FROM Customer IC 
    WHERE IC.customerID = OC.customerID) 
FROM Customer OC 
3

partecipare al tavolo a una versione riassunta di se stesso:

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 
5

Eccolo:

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 
3

È possibile utilizzare una sottoquery per ottenere la somma totale e quindi aggiungerla alla tabella per aggiungere le altre colonne.

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 

Vedi SQL Fiddle with Demo

2

Assumendo che il DBMS è MS Sql-Server, è possibile utilizzare un SUM(Payments) con OVER clausola:

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

SQL-Fiddle: http://sqlfiddle.com/#!3/2ac38/2/0

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 
4

Prova questo, (sarà per lo più funziona su qualsiasi 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 Demo

Problemi correlati