Search code examples
sqllibreoffice-base

OpenOffice / LibreOffice Base SQL Group By


I have a small LibreOffice database with customer data and invoices. Each invoice consists of n items.

Customers:

**********************
* ID * Name * Adress *
**********************
*  0 *   N1 *     A1 *
*  1 *   N2 *     A2 *
**********************

Invoices:

********************************
* ID * CustomerID *       Date *
********************************
*  0 *          0 * 01/01/1970 *
*  1 *          0 * 08/02/1971 *
********************************

Items:

***********************************
* ID * InvoiceID * Amount * Price *
***********************************
*  1 *         0 *     12 * 12.95 *
*  2 *         0 *      9 *  8.75 *
*  3 *         1 *     29 *  8.75 *
***********************************

I want to create a query with the value of an invoice:

SELECT "invoices"."ID", SUM( "items"."Amount" * "items"."Price" ) 
FROM "invoices", "items" 
WHERE "invoices"."ID" = "items"."InvoiceID" 
GROUP BY "invoice"."ID"

So far everything works. But if I want to include some more information to the query, e.g.

SELECT "invoices"."ID", "invoices"."Date", SUM( "items"."Amount" * "items"."Price" ) 
FROM "invoices", "items" 
WHERE "invoices"."ID" = "items"."InvoiceID" 
GROUP BY "invoice"."ID"

I get the error message Not in aggregate function or group by clause. I tried it in phpmyadmin and it worked, so I think, that the SQL Syntax should be ok. What is the problem with my SQL statement?


Solution

  • You have to include "invoices"."Date" in the group by clause.