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
https://forums.phpfreaks.com/topic/258516-mysql-query-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.