OpenOffice / LibreOffice基本SQL组

时间:2016-01-26 14:41:58

标签: sql libreoffice-base

我有一个包含客户数据和发票的小型LibreOffice数据库。每张发票由n个项目组成。

客户:

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

发票:

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

产品:

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

我想创建一个包含发票价值的查询:

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

到目前为止一切正常。但是,如果我想在查询中包含更多信息,例如

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

我收到错误消息Not in aggregate function or group by clause。我在phpmyadmin中尝试了它并且它有效,所以我认为,SQL语法应该没问题。我的SQL语句有什么问题?

1 个答案:

答案 0 :(得分:2)

您必须在group by子句中包含“发票”。“日期”。