如何简化此SQL语句

时间:2016-10-13 01:15:37

标签: mysql sql

我在下面有这个查询...这些是输入过滤器($ P {start_date})但是它目前在Jaspersoft Studio中给我一个错误。

我发现问题可能出在我的FROM标准上。我使用了JOIN而不是INNER JOIN,但它没有返回任何结果。

我想知道是否有任何方法可以简化此查询。

SELECT
LOCATIONS.ID AS LOCATIONID,
LOCATIONS.NAME AS LOCATIONNAME,
PRODUCTS.REFERENCE,
PRODUCTS.CODE AS BARCODE,
PRODUCTS.NAME AS ITEM_NAME,
STOCKDIARY.ATTRIBUTESETINSTANCE_ID,
ATTRIBUTESETINSTANCE.DESCRIPTION AS ATTINSTANCEDESC,
PRODUCTS.CATEGORY,
TICKETLINES.UNITS,
CATEGORIES.NAME AS CATEGORYNAME,
STOCKDIARY.DATENEW,
STOCKDIARY.REASON,
SUM(CASE WHEN STOCKDIARY.UNITS <0 AND STOCKDIARY.DATENEW >=  $P{start_date} AND (STOCKDIARY.DATENEW) <= $P{end_date} THEN STOCKDIARY.UNITS ELSE 0 END) AS UNITSOUT,
SUM(CASE WHEN STOCKDIARY.UNITS >=0 AND STOCKDIARY.DATENEW >=  $P{start_date} AND (STOCKDIARY.DATENEW) <= $P{end_date} THEN STOCKDIARY.UNITS ELSE 0 END) AS UNITSIN,
SUM(CASE WHEN STOCKDIARY.DATENEW <= $P{end_date} THEN STOCKDIARY.UNITS ELSE 0 END) AS STOCKTOTALNEW,
SUM(CASE WHEN STOCKDIARY.DATENEW <=  $P{start_date} THEN STOCKDIARY.UNITS ELSE 0 END) AS STARTINGBALANCE,
SUM(TICKETLINES.UNITS) AS UNIT


FROM TICKETLINES,
STOCKDIARY
LEFT JOIN
LOCATIONS ON STOCKDIARY.LOCATION = LOCATIONS.ID
INNER JOIN
PRODUCTS ON PRODUCTS.ID = STOCKDIARY.PRODUCT
LEFT JOIN
CATEGORIES ON PRODUCTS.CATEGORY = CATEGORIES.ID
LEFT JOIN
ATTRIBUTESETINSTANCE ON STOCKDIARY.ATTRIBUTESETINSTANCE_ID  = ATTRIBUTESETINSTANCE.ID


WHERE products.name LIKE "%"

GROUP BY LOCATIONS.NAME , PRODUCTS.REFERENCE , STOCKDIARY.ATTRIBUTESETINSTANCE_ID , ATTRIBUTESETINSTANCE.DESCRIPTION
ORDER BY PRODUCTS.NAME

0 个答案:

没有答案