Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to sum values of a table of my database, and I wrote:

strQuery = _T( "SELECT SUM (pezzi) FROM PRODOTTI " );


but it says that in my database there isn't database.sum...I don't undestand, because it must recogniza functiobn sum

What I have tried:

I tried to search in internet, but they use function sum without problems
Posted
Updated 11-Oct-19 4:51am

Check your connection string and make sure you are connecting to a specific DB, rather than just connecting to the server.

If you're sure that's right, then use the debugger to check exactly what you are doing with that query string and exactly what you are passing to SQL. (We don't have access to the rest of your code, so we can't check that the string does not get modified from that point on).

SELECT SUM(... works fine on all my systems here, so I'd guess it's something you are doing to the the string later.
 
Share this answer
 
Comments
Member 14594285 11-Oct-19 11:35am    
now query works, but a question, when I write code to write my results in a list, I write:
recordset.GetFieldValue(_T("data_scanner"), dtScanner);
recordset.GetFieldValue(_T("somma"), lPezzi);
pLst->SetItemText(nProdotti, 1, strDataScanner);

but there isn't group by data..there is only list of dates
OriginalGriff 11-Oct-19 11:40am    
Separate problem, separate question - post it as a new one, so it's open to all here.
And include the full code for the relevant fragment so we all know exactly what you are working with.
Member 14594285 11-Oct-19 11:47am    
ok
You need to use a GROUP BY clause when you use any aggregation function:
x
strQuery = _T( "SELECT SUM (pezzi) FROM PRODOTTI GROUP BY id " );
 
Share this answer
 
v3
Comments
Member 14594285 11-Oct-19 10:29am    
I wrote:
strQuery = _T( "SELECT SUM (pel_pezzi) FROM PRODOTTI GROUP BY PRODOTTI.data_scanner");
if (CFG->m_bDataScanner)
{
strQuery.Append(bWhere ? _T("AND ") : _T("WHERE "));
strQuery.AppendFormat(_T("PRODOTTI.pel_data_scanner BETWEEN '%04ld-%02ld-%02ld %02ld:%02ld:%02ld' AND '%04ld-%02ld-%02ld %02ld:%02ld:%02ld'"),
CFG->m_dtScannerIni.GetYear(), CFG->m_dtScannerIni.GetMonth(), CFG->m_dtScannerIni.GetDay(), CFG->m_dtScannerIni.GetHour(), CFG->m_dtScannerIni.GetMinute(), CFG->m_dtScannerIni.GetSecond(),
CFG->m_dtScannerFin.GetYear(), CFG->m_dtScannerFin.GetMonth(), CFG->m_dtScannerFin.GetDay(), CFG->m_dtScannerFin.GetHour(), CFG->m_dtScannerFin.GetMinute(), CFG->m_dtScannerFin.GetSecond());
bWhere = TRUE;
}
Member 14594285 11-Oct-19 10:30am    
but it doesn't work, there is an error in my sintax
phil.o 11-Oct-19 10:33am    
Well, it could be interesting to see the final query which is produced. The only thing I can say right now is that there seems to be missing a whitespace after data_scanner. This is a common mistake when building sql queries by concatenation.
Member 14594285 11-Oct-19 10:38am    
I wrote only this to try:
strQuery = _T( "SELECT SUM (pel_pezzi), data_scanner FROM PRODOTTI GROUP BY PRODOTTI.data_scanner ");
but it doesn't wotk, it says that in my database there the column SUM, I don't understand
Member 14594285 11-Oct-19 10:46am    
now I removed space after SUM and there is an other error, "impossibile trovare l'oggetto desiderato"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900