2013-05-25 40 views
0

當我在mysql編輯器上運行它時發生以下查詢,它在jasper報告中使用它時給出「group by by invalid by error」的錯誤,它不會產生任何錯誤?查詢有什麼問題?在mySql中使用Group無效

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 
+0

你在一個叫做'RECEIPTS'表中增加條件,但它不是隨時隨地加入。 –

回答

3

這似乎是因爲你正在做的profitloss推導之內的總和;這裏還有一種可複製的join條件where子句中的條件,另一個別名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 
+0

哇,這令人驚訝的運行,但我不得不添加RECEIPT.ID後LEGP OUTER JOIN中的CATEGORIES.ID,雖然我不知道原因... – Saqib

0

你應該有選擇組內的所有組字段:

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 
+0

同樣的錯誤bro:| – Saqib