this is my query:
$sql = "SELECT sum(CommitmentItemInvoices.Amount) AS PendTotal, CommitmentItemInvoices.Amount AS Amount_Pending , CommitmentItems.*, CommitmentItemInvoices.*
FROM CommitmentItemInvoices
LEFT JOIN CommitmentItems on CommitmentItems.commitmentItemId = CommitmentItemInvoices.commitmentItemId
WHERE (CommitmentItemInvoices.Status='new' AND CommitmentItemInvoices.commitmentItemId = :commitId)";
try{
//prepare statement
$stmt = $con->prepare ($sql);
//bind values to :username
$stmt->bindValue("commitId", $commitId, PDO::PARAM_STR);
$stmt->execute();
}catch(PDOException $e){
echo "Error: ".$e->getMessage();
}
I am getting the following error:
Error: SQLSTATE[42000]: Syntax error or access violation: 8120 [Microsoft][ODBC SQL Server Driver][SQL Server]Column 'CommitmentItemInvoices.Amount' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. (SQLExecute[8120] at ext\pdo_odbc\odbc_stmt.c:254)
Any idea as to what I am doing wrong in the SQL query?
SQL Server
? Your issue is that you have left out thegroup by
clause. You need to include those columns not included in an aggregate function in thegroup by
clause. – sgeddesCommitmentItemInvoices.Amount AS Amount_Pending , CommitmentItems.*, CommitmentItemInvoices.*
Now just add back in the amount one.. does it work? No? Look at how aggregates work and the fact they need group by. mySQL extends the group by so that it's not always needed, but SQL Server doesn't. – xQbert