0

当我在 mysql 编辑器上运行它时,我有以下查询,它在 jasper 报告中使用它时给出“无效使用 group by”的错误,它不会产生任何错误?查询有什么问题?

SELECT CUSTOMERS.TAXID, 
CUSTOMERS.NAME AS CUSTOMER, 
CATEGORIES.NAME AS CATEGORY,
PRODUCTS.REFERENCE, 
PRODUCTS.NAME AS PRODUCT, 
SUM(TICKETLINES.UNITS) AS UNIT, 
SUM(TICKETLINES.UNITS * TICKETLINES.PRICE) AS TOTAL, 
SUM(TICKETLINES.UNITS * TICKETLINES.PRICE) / SUM(TICKETLINES.UNITS) AS MEANPRICE,
PRODUCTS.PRICEBUY AS BUYPRICE,
((SUM(SUM(TICKETLINES.PRICE) / SUM(TICKETLINES.UNITS)) - PRODUCTS.PRICEBUY) * TICKETLINES.UNITS) AS PROFITLOSS
FROM TICKETS 
LEFT OUTER JOIN CUSTOMERS ON TICKETS.CUSTOMER = CUSTOMERS.ID, TICKETLINES 
LEFT OUTER JOIN PRODUCTS ON TICKETLINES.PRODUCT = PRODUCTS.ID 
LEFT OUTER JOIN CATEGORIES ON PRODUCTS.CATEGORY = CATEGORIES.ID
WHERE RECEIPTS.ID = TICKETS.ID AND 
TICKETS.ID = TICKETLINES.TICKET AND 
CATEGORIES.ID = PRODUCTS.CATEGORY
GROUP BY CUSTOMERS.ID, CATEGORIES.ID, PRODUCTS.ID
ORDER BY CUSTOMERS.NAME, 
CATEGORIES.NAME, 
PRODUCTS.NAME
4

2 回答 2

3

这似乎是因为您在利润推导中进行了一个总和内的总和;where子句中还有一个条件复制一个join条件,另一个复制到receipt别处未提及的别名 - 相反,请尝试:

SELECT CUSTOMERS.TAXID, 
       CUSTOMERS.NAME AS CUSTOMER, 
       CATEGORIES.NAME AS CATEGORY,
       PRODUCTS.REFERENCE, 
       PRODUCTS.NAME AS PRODUCT, 
       SUM(TICKETLINES.UNITS) AS UNIT, 
       SUM(TICKETLINES.UNITS * TICKETLINES.PRICE) AS TOTAL, 
       SUM(TICKETLINES.UNITS * TICKETLINES.PRICE) / SUM(TICKETLINES.UNITS) AS MEANPRICE,
       PRODUCTS.PRICEBUY AS BUYPRICE,
       SUM( (TICKETLINES.PRICE - PRODUCTS.PRICEBUY) * TICKETLINES.UNITS ) AS PROFITLOSS
FROM TICKETS 
JOIN TICKETLINES ON TICKETS.ID = TICKETLINES.TICKET
LEFT OUTER JOIN CUSTOMERS ON TICKETS.CUSTOMER = CUSTOMERS.ID
LEFT OUTER JOIN PRODUCTS ON TICKETLINES.PRODUCT = PRODUCTS.ID 
LEFT OUTER JOIN CATEGORIES ON PRODUCTS.CATEGORY = CATEGORIES.ID
GROUP BY CUSTOMERS.ID, CATEGORIES.ID, PRODUCTS.ID
ORDER BY CUSTOMERS.NAME, CATEGORIES.NAME, PRODUCTS.NAME
于 2013-05-25T17:44:24.953 回答
0

您应该在 SELECT 组中包含所有组字段:

SELECT CUSTOMERS.ID, CATEGORIES.ID, PRODUCTS.ID,CUSTOMERS.TAXID, 
CUSTOMERS.NAME AS CUSTOMER, 
CATEGORIES.NAME AS CATEGORY,
PRODUCTS.REFERENCE, 
PRODUCTS.NAME AS PRODUCT, 
SUM(TICKETLINES.UNITS) AS UNIT, 
SUM(TICKETLINES.UNITS * TICKETLINES.PRICE) AS TOTAL, 
SUM(TICKETLINES.UNITS * TICKETLINES.PRICE) / SUM(TICKETLINES.UNITS) AS MEANPRICE,
PRODUCTS.PRICEBUY AS BUYPRICE,
((SUM(SUM(TICKETLINES.PRICE) / SUM(TICKETLINES.UNITS)) - PRODUCTS.PRICEBUY) * TICKETLINES.UNITS) AS PROFITLOSS
FROM TICKETS 
LEFT OUTER JOIN CUSTOMERS ON TICKETS.CUSTOMER = CUSTOMERS.ID, TICKETLINES 
LEFT OUTER JOIN PRODUCTS ON TICKETLINES.PRODUCT = PRODUCTS.ID 
LEFT OUTER JOIN CATEGORIES ON PRODUCTS.CATEGORY = CATEGORIES.ID
WHERE RECEIPTS.ID = TICKETS.ID AND 
TICKETS.ID = TICKETLINES.TICKET AND 
CATEGORIES.ID = PRODUCTS.CATEGORY
GROUP BY CUSTOMERS.ID, CATEGORIES.ID, PRODUCTS.ID
ORDER BY CUSTOMERS.NAME, 
CATEGORIES.NAME, 
PRODUCTS.NAME
于 2013-05-25T17:31:57.767 回答