If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > SUM problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-01-04, 08:22
Panavision Panavision is offline
Registered User
 
Join Date: Jan 2003
Posts: 15
SUM problem

Code:
SQL = "SELECT * FROM PurchaseCardCostCodes "
SQL = SQL & " INNER JOIN PurchaseCardTransactionLog ON PurchaseCardCostCodes.costcode_id 
= PurchaseCardTransactionLog.costcode_id "
SQL = SQL & " WHERE PurchaseCardTransactionLog.tl_date BETWEEN '"&DateOne&"' AND '"&DateTwo&"'"
SQL = SQL & " AND PurchaseCardCostCodes.costcode_owner = '"&User&"'" 
SQL = SQL & " AND PurchaseCardCostCodes.costcode_id = '"&RadioButtonVal&"'"
SQL = SQL & " SELECT SUM(tl_net) AS nettot FROM PurchaseCardTransactionLog"
The ASP code doesn't recognise:

Code:
<% nettot = objRec("nettot").Value%>
<td><%=nettot%></td>
The Error is :
Item cannot be found in the collection corresponding to the requested name or ordinal.

I've tried the SUM function on its own, and it works fine, but part of a bigger SQL statement and I get problems
Reply With Quote
  #2 (permalink)  
Old 03-01-04, 08:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
the sql you have does not make sense

it looks like two separate sql statements just smooshed together
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-01-04, 08:55
Panavision Panavision is offline
Registered User
 
Join Date: Jan 2003
Posts: 15
Yeah, I guess changing the last line to

SQL = SQL & " SUM(PurchaseCardTransactionLog.tl_net) AS nettot FROM PurchaseCardTransactionLog"

won't work either
Reply With Quote
  #4 (permalink)  
Old 03-01-04, 10:09
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
okay, i think i see where you want to go with that

try this:
PHP Code:
select CC.col1
     
CC.col2
     
CC.col3
     
sum(TL.tl_net) as nettot
  from PurchaseCardCostCodes  CC
inner
  join PurchaseCardTransactionLog TL
    on CC
.costcode_id 
     
TL.costcode_id 
 where TL
.tl_date between [i]DateOne[/i
                      and [
i]DateTwo[/i]
   and 
CC.costcode_owner = [i]User[/i
   and 
CC.costcode_id = [i]RadioButtonVal[/i]
group
    by CC
.col1
     
CC.col2
     
CC.col3 
since you are grouping TL rows to each CC row, you cannot use SELECT *

you need to list the columns you want in the SELECT list and also the GROUP BY clause
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 03-01-04, 10:48
Panavision Panavision is offline
Registered User
 
Join Date: Jan 2003
Posts: 15
Hmm, had a go, but I get more errors:

Ambiguous column name 'costcode_id'.


The bold bit is not from the costcode table, that's where the error is, I think.

Code:
SQL = "SELECT costcode_id, costcode_bc, costcode_unit, costcode_activity, costcode_location, costcode_dept, costcode_owner, SUM(PurchaseCardTransactionLog.tl_net) AS nettot 

FROM PurchaseCardCostCodes"

SQL = SQL & " INNER JOIN PurchaseCardTransactionLog ON PurchaseCardCostCodes.costcode_id = PurchaseCardTransactionLog.costcode_id"


SQL = SQL & " WHERE PurchaseCardTransactionLog.tl_date BETWEEN '"&DateOne&"' AND '"&DateTwo&"'"

SQL = SQL & " AND PurchaseCardCostCodes.costcode_owner = '"&User&"'" 

SQL = SQL & " AND PurchaseCardCostCodes.costcode_id = '"&RadioButtonVal&"'"

SQL = SQL & " GROUP BY costcode_id, costcode_bc, costcode_unit, costcode_activity, costcode_location, costcode_dept, costcode_owner"
Reply With Quote
  #6 (permalink)  
Old 03-01-04, 10:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
it's the very first column after the SELECT keyword

there are two columns in your query with that name

you need to qualify which one you want:

SELECT PurchaseCardCostCodes.costcode_id, ...
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 03-02-04, 05:05
Panavision Panavision is offline
Registered User
 
Join Date: Jan 2003
Posts: 15
Thumbs up

Getting there...but still probs:

Code:
SQL = "SELECT PurchaseCardTransactionLog.costcode_id, 
PurchaseCardCostCodes.costcode_bc, 
PurchaseCardCostCodes.costcode_unit, 
PurchaseCardCostCodes.costcode_activity, 
PurchaseCardCostCodes.costcode_location, 
PurchaseCardCostCodes.costcode_dept, 
PurchaseCardCostCodes.costcode_owner, SUM
(PurchaseCardTransactionLog.tl_net) AS nettot FROM PurchaseCardCostCodes "


SQL = SQL & " INNER JOIN PurchaseCardTransactionLog ON
PurchaseCardCostCodes.costcode_id = 
PurchaseCardTransactionLog.costcode_id"

SQL = SQL & " WHERE PurchaseCardTransactionLog.tl_date 
BETWEEN '"&DateOne&"' AND '"&DateTwo&"'"

SQL = SQL & " AND PurchaseCardCostCodes.costcode_owner 
= '"&User&"'" 

SQL = SQL & " AND PurchaseCardCostCodes.costcode_id 
= '"&RadioButtonVal&"'"

SQL = SQL & " GROUP BY 
PurchaseCardTransactionLog.costcode_id, 
PurchaseCardCostCodes.costcode_bc, PurchaseCardCostCodes.costcode_unit, 
PurchaseCardCostCodes.costcode_activity, 
PurchaseCardCostCodes.costcode_location, 
PurchaseCardCostCodes.costcode_dept, 
PurchaseCardCostCodes.costcode_owner"
I can output all fields from costcode but not TransactionLog.
If I add fields from TL to the SELECT and GroupBy statements, makes no difference.
Reply With Quote
  #8 (permalink)  
Old 03-02-04, 06:21
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
if you add fields from TransactionLog, you can no longer do a SUM() over the TransactionLog grouped rows

i think you need to study grouping a bit more



__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On