Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to sum records with same id in query

Status
Not open for further replies.

Attievv

Programmer
Apr 7, 2011
1
ZA
This is the result set on the query run below
---------------------------------------------------------------
TDP000499 RS 002259 PAYMENT - THANK YOU TDP000499 50.00
TDP000499 RS 002261 PAYMENT - THANK YOU TDP000499 80.00
TDP000499 RS 002262 PAYMENT - THANK YOU TDP000499 60.00
TDP000499 RS 002260 PAYMENT - THANK YOU TDP000499 220.00
TDP000500 RS 002260 PAYMENT - THANK YOU TDP000500 770.00
TDP000500 RS 002268 PAYMENT - THANK YOU TDP000500 80.00
TDP000501 RS 002268 44574 580.00
TDP000502 RS 002268 PAYMENT - THANK YOU TDP000502 190.00
TDP000502 RS 002270 PAYMENT - THANK YOU TDP000502 199.99

Column 1 = Payment serial
Column 2 = inv number paid(not important)
Column 3 = payment description
Column 4 = Amount paid

The purpose here is to group the data by column 1 (i.e. TDP00049 must give me one row with the total value of column 4 for the payment serial i.e. tdp000499 must sum = 410.00 ) Currently the bottom query gives me 50.00. It does not sum all 4 rows.

How can I change the query to get the correct result?

sTmp = "SELECT c.AccountNumber, " + vbCrLf
sTmp = sTmp + " c.CompanyName, " + vbCrLf
sTmp = sTmp + " ctx.TransactionDate, " + vbCrLf
sTmp = sTmp + " ctx.TransactionNumber, " + vbCrLf
sTmp = sTmp + " cs.OrderNumber, " + vbCrLf
sTmp = sTmp + " ctx.ReferenceNumber, " + vbCrLf

Select Case CustTypeID
Case Is = 1
sTmp = sTmp + " IF(LEFT(ctx.TransactionNumber,3) = 'TDP', IFNULL(ctx.PaymentDescription,ctx.TransactionDescription),
ctx.TransactionDescription) AS TransactionDescription, " + vbCrLf
Case Is = 2
sTmp = sTmp + " IF(LEFT(ctx.TransactionNumber,3) = 'IDP', IFNULL(ctx.PaymentDescription,ctx.TransactionDescription),
ctx.TransactionDescription) AS TransactionDescription, " + vbCrLf
End Select

sTmp = sTmp + " ctx.DebitAmount), ctx.CreditAmount " + vbCrLf
sTmp = sTmp + "FROM cust_trans ctx LEFT JOIN cust c ON (c.CustomerID = ctx.CustomerID) " + vbCrLf
sTmp = sTmp + " LEFT JOIN cust_sales cs ON (cs.TransactionNumber = ctx.TransactionNumber) " + vbCrLf
sTmp = sTmp + "WHERE ctx.TransactionDate BETWEEN '" & Format(Date1, "yyyy-MM-dd") & "' AND '" & Format(Date2, "yyyy-MM-dd") & "' AND " + vbCrLf
sTmp = sTmp + " c.AccountNumber BETWEEN '" & FormatSQL(String1) & "' AND '" & FormatSQL(String2) & "' AND " + vbCrLf
sTmp = sTmp + " c.CustTypeID = '" & CustTypeID & "' And " + vbCrLf
sTmp = sTmp + " ctx.TransactionDescription NOT LIKE '%Period Balancing%' " + vbCrLf
sTmp = sTmp + "GROUP BY ctx.TransactionNumber " + vbCrLf
sTmp = sTmp + "ORDER BY TransactionDate ASC, ctx.TransactionID ASC, TransactionNumber ASC, AccountNumber ASC;" + vbCrLf
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top