Jump to content

MySQL Query Help


imperium2335

Recommended Posts

Hi,

 

I have a query that calculates three totals in three different currencies, so there are 3 rows output each with 3 columns.

 

$result = $dbh->query("SELECT SUM(((SELECT SUM((quantity*(sellingNet+sellingVat)))
							FROM parts_trading
							WHERE parts_trading.enquiryRef = enquiries.id AND parts_trading.sellingCurrency = 1)+(SELECT SUM(feeAmountNet+feeAmountVat)
							FROM enquiries_custom_fees
							WHERE enquiries_custom_fees.enquiryRef = enquiries.id))) AS salesTotalPounds,
					  SUM(((SELECT SUM((quantity*(sellingNet+sellingVat)))
							FROM parts_trading
							WHERE parts_trading.enquiryRef = enquiries.id AND parts_trading.sellingCurrency = 2)+(SELECT SUM(feeAmountNet+feeAmountVat)
							FROM enquiries_custom_fees
							WHERE enquiries_custom_fees.enquiryRef = enquiries.id))) AS salesTotalDollars,
					  SUM(((SELECT SUM((quantity*(sellingNet+sellingVat)))
							FROM parts_trading
							WHERE parts_trading.enquiryRef = enquiries.id AND parts_trading.sellingCurrency = 3)+(SELECT SUM(feeAmountNet+feeAmountVat)
							FROM enquiries_custom_fees
							WHERE enquiries_custom_fees.enquiryRef = enquiries.id))) AS salesTotalEuros,
					  		
						 (SELECT SUM(jobs_payments.amount) FROM jobs_payments WHERE currencyRef = 1 AND jobRef = jobs.id) AS paidAmountPounds,

						 	SUM(((SELECT SUM((quantity*(sellingNet+sellingVat)))
							FROM parts_trading
							WHERE parts_trading.enquiryRef = enquiries.id)+(SELECT SUM(feeAmountNet+feeAmountVat)
							FROM enquiries_custom_fees
							WHERE enquiries_custom_fees.enquiryRef = enquiries.id)-jobs_payments.amount)) AS balanceTotal
						 FROM jobs
						 LEFT JOIN jobs_payments ON jobs.id = jobs_payments.jobRef,
						 enquiries, users, branches, entity_details,
						 (SELECT currencyRef, SUM(amount) AS total FROM jobs_payments GROUP BY currencyRef) AS paidTotalsTable
						 WHERE enquiries.traderRef = users.id
						 AND jobs.enquiryRef = enquiries.id
						 AND users.branchRef = branches.id
						 AND enquiries.entityRef = entity_details.id
						 AND entity_details.paymentTermsRef = 1
						 AND (SELECT SUM(quantity*(sellingNet+sellingVat))
							FROM parts_trading
							WHERE parts_trading.enquiryRef = enquiries.id) != ''
						 AND ((SELECT SUM((quantity*(sellingNet+sellingVat)))
							FROM parts_trading
							WHERE parts_trading.enquiryRef = enquiries.id)+(SELECT SUM(feeAmountNet+feeAmountVat)
							FROM enquiries_custom_fees
							WHERE enquiries_custom_fees.enquiryRef = enquiries.id)) != jobs_payments.amount
						 AND ((SELECT SUM((quantity*(sellingNet+sellingVat)))
							FROM parts_trading
							WHERE parts_trading.enquiryRef = enquiries.id)+(SELECT SUM(feeAmountNet+feeAmountVat)
							FROM enquiries_custom_fees
							WHERE enquiries_custom_fees.enquiryRef = enquiries.id)-jobs_payments.amount) > 0
						 AND jobs.stateRef != 5") ;

 

The problem is, is that 'paidAmountPounds' only shows the value of the first row in that quiery (£40, but should by much much more).

 

Does any one know how I can fix this?

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.