Jump to content

Get 3 totals seperately problem


imperium2335

Recommended Posts

Could someone tell me why this isnt working:

 

$branchPoundsResult = mysql_query("(SELECT branch, currency, sum(absoluteTotal) as poundTotal
								   FROM invoices_out
								   WHERE branch = '$currentBranch'
								   AND currency = '£')
								   UNION
								   (SELECT branch, currency, sum(absoluteTotal) as dollarTotal
								   FROM invoices_out
								   WHERE branch = '$currentBranch'
								   AND currency = '$')
								   UNION
								   (SELECT branch, currency, sum(absoluteTotal) as euroTotal
								   FROM invoices_out
								   WHERE branch = '$currentBranch'
								   AND currency = '€')
								   ")or die(mysql_error()) ;

 

The desired output is three rows for whatever the current branch is, each with a total in pounds, euros and dollars, but im getting nothing out :(

Link to comment
Share on other sites

The desired output is three rows for whatever the current branch is, each with a total in pounds, euros and dollars, but im getting nothing out :(

You're not outputting anything.  Is there more code?  If the or die() is not printing anything, than your query is syntactically correct.

Link to comment
Share on other sites

Sorry, this is what follows it (branchPoundsResult is just a typo):

 

$currencyRows = mysql_fetch_assoc($branchPoundsResult) ;

echo $currencyRows ['poundTotal'] . "<br />"; // The total in euros.
echo $currencyRows ['dollarTotal'] . "<br />"; // The total in euros.
echo $currencyRows ['euroTotal'] . "<br />"; // The total in euros.

exit() ;

 

The exit is there because this is part of a loop (there are more branches it will go through, this is just for testing to see if I can get the right output first).

Link to comment
Share on other sites

Hi,

 

Here is the full code for this stage:

 

include("currency-convert.php") ;
include("dbconnectlocal.php") ;

$invOutBranchesResults = mysql_query("SELECT DISTINCT branch
									  FROM invoices_out") ; // Get the branches that have invoices out to customers one by one.
while($branchRow = mysql_fetch_assoc($invOutBranchesResults)) {
// For each branch...
echo $currentBranch = $branchRow['branch'] ;
// For this branch, we now add up all their outbound invoices for each currency, for $s and €s we need to convert to pounds, then add up the totals for each currency, to give us the total in pounds.

$branchPoundsResult = mysql_query("SELECT branch, currency, sum(absoluteTotal)
								   FROM invoices_out
								   WHERE branch = '$currentBranch'
								   AND currency = '£'
								   GROUP BY branch
								   ")or die(mysql_error()) ;
$branchDollarsResult = mysql_query("SELECT branch, currency, sum(absoluteTotal)
								   FROM invoices_out
								   WHERE branch = '$currentBranch'
								   AND currency = '$'
								   GROUP BY branch
								   ")or die(mysql_error()) ;
$branchEurosResult = mysql_query("SELECT branch, currency, sum(absoluteTotal)
								   FROM invoices_out
								   WHERE branch = '$currentBranch'
								   AND currency = '€'
								   GROUP BY branch
								   ")or die(mysql_error()) ;
// Get all the £,$,€ invoice totals for this branch, returning one row which is the total.
$poundsRow = mysql_fetch_assoc($branchPoundsResult) ;
$dollarsRow = mysql_fetch_assoc($branchDollarsResult) ;
$eurosRow = mysql_fetch_assoc($branchEurosResult) ;

$pounds = $poundsRow['sum(absoluteTotal)'] ; // The total in pounds.
$dollars = $dollarsRow['sum(absoluteTotal)'] ; 
$euros = $eurosRow['sum(absoluteTotal)'] ;

// Convert the dollars and euros into pounds...

$dinPounds = cConvert($dollars, 'dollar') ;
$einPounds = cConvert($euros, 'euro') ;

$finalPoundsTotal = $pounds + $dinPounds + $einPounds ;
}

 

As you can see, at the moment I'm using three separate quires for the different currency. I'm thinking there must be a way to do this with just one query.

 

Thanks for your input so far. :)

Link to comment
Share on other sites

that was my guess  ;)

 

this select should replace ALL the select that you have (even the first one)

 

SELECT branch, 
       sum(if(currency = '£',absoluteTotal,0)) AS branchpound,
       sum(if(currency = '$',absoluteTotal,0)) AS branchdollar,
       sum(if(currency = '€',absoluteTotal,0)) AS brancheuro
  FROM invoice_out
  GROUP BY branch	

 

the rest is only display... try it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.