0

I am using the following statement to develop a simple query. Unfortunately I continually get a syntax error when I run the page. If I take out either of the RaisedInvoice Select or the Transaction Sum elements it works but fails when both are in. It must be something stupid so, can I please apologise in advance for my silly mistake.

SELECT 
RaisedInvoices.[Invoice Number], SUM(Transactions.sellgross) AS SumOfsellgross 
FROM (RaisedInvoices LEFT JOIN Customers ON RaisedInvoices.[Customer WORef] = Customers.WORef) 
LEFT JOIN Transactions ON RaisedInvoices.[Invoice Number] = Transactions.invnumber
2
  • PDO doesn't execute your queries. You are getting a Syntax error from your database server. Commented Apr 8, 2021 at 13:03
  • Thanks. I did realise that but worded it badly. Commented Apr 9, 2021 at 15:03

2 Answers 2

2

You are aggregating one column but not the other one. In this case, you need to include a GROUP BY clause with the non-aggregated column(s).

For example:

SELECT 
  RaisedInvoices.[Invoice Number], -- not aggegated
  SUM(Transactions.sellgross) AS SumOfsellgross -- aggregated
FROM RaisedInvoices 
LEFT JOIN Customers 
      ON RaisedInvoices.[Customer WORef] = Customers.WORef
LEFT JOIN Transactions 
      ON RaisedInvoices.[Invoice Number] = Transactions.invnumber
GROUP BY RaisedInvoices.[Invoice Number] -- added GROUP BY here
Sign up to request clarification or add additional context in comments.

Comments

0

Based on your query, I would say that you don't need the join with the Customers table.

Here is how I would write it:

$query = "SELECT RaisedInvoices.[Invoice Number], SUM(Transactions.sellgross) AS SumOfsellgross FROM RaisedInvoices LEFT JOIN Transactions ON RaisedInvoices.[Invoice Number] = Transactions.invnumber";

However, I'm not sure that the brackets syntax is something that the PDO will accept.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.