ORDER BY导致查询不起作用?

时间:2016-07-26 13:51:58

标签: php sql codeigniter advantage-database-server

我有一个在访问中按预期工作的查询。我已经“转换”了一个它不想工作的网络系统。但是,当我删除所需的ORDER BY(在发布的示例的最底部)时,查询正常工作。

我不确定可能导致问题的原因。出现的错误消息如下: Expected lexical element not found: ) There was a problem parsing the table names after the FROM keyword in your SELECT statement. -- Location of error in the SQL statement is: 812 (line: 17 column: 72)

我不想发布整个查询,因为它很长(除非请求),但问题出现在底线:

    SELECT TOP 5 [90Days].PRODUCT, [90Days].product2, [90Days].pcustkey, [90Days].[90DayThroughput], TABLETHREE.SumOfLQUAN AS [Current Stock], (ROUND([SumOfLQUAN]/[90DayThroughput],3))*90 AS [DaysRemaining] FROM
    (
        SELECT TABLEONE.PRODUCT, Sum(TABLEONE.QUANTITY) AS [90DayThroughput], TABLETWO.product2, TABLETWO.pcustkey FROM TABLETWO INNER JOIN TABLEONE ON TABLETWO.PRODCODE = TABLEONE.PRODUCT WHERE
        (
            (
                (TABLEONE.DATE)>='$date'-90
            )
            AND
            (
                (TABLEONE.type)='I11' Or (TABLEONE.type)=' 11'
            )
            AND
            (
                (TABLETWO.POWNER)='$custCode'
            )
        )
        GROUP BY TABLEONE.PRODUCT, TABLETWO.product2, TABLETWO.pcustkey ORDER BY Sum(TABLEONE.QUANTITY) DESC

非常感谢任何帮助。如果需要,我可以提供更多查询。

1 个答案:

答案 0 :(得分:0)

我认为如果在按照添加订单之前工作正常,我会错过一些查询 没有加入TABLETHREE

我创建了以下内容,因此我可以运行查询

TRY
DROP TABLE #Table1;
CATCH ADS_SCRIPT_EXCEPTION
IF __errcode = 7112 THEN
--Do Nothing
ELSE
RAISE; // re-raise the exception
END IF;
END TRY;

TRY
DROP TABLE #Table2;
CATCH ADS_SCRIPT_EXCEPTION
IF __errcode = 7112 THEN
--Do Nothing
ELSE
RAISE; // re-raise the exception
END IF;
END TRY;

Select 'AAA' as Product, 12 as Quantity, 'I11' as Type, curdate() as  "Date"
into #Table1
from System.iota;

Select 'AAA' as Product, 'AAA111' as CustKey, '$custCode' as POwner, 'MyPRODCODE' as PRODCODE, 'KEY' as pcustkey
into #Table2
from System.iota;

我已经注释掉了TABLETHREE的东西 你需要在你的小组之后有一个“90天” 并且我们通过ORDER BY [90天]更改了订单。[90DayThroughput] DESC

SELECT TOP 5 
[90Days].PRODUCT, 
[90Days].product2, 
[90Days].pcustkey, 
[90Days].[90DayThroughput] 
--,#Table3.SumOfLQUAN AS [Current Stock], 
--(ROUND([SumOfLQUAN]/[90DayThroughput],3))*90 AS [DaysRemaining] 
FROM
    (
        SELECT tb1.PRODUCT, 
        Sum(tb1.QUANTITY) AS [90DayThroughput], 
        tb2.PRODCODE as product2, 
        tb2.pcustkey 
        FROM #Table2 tb2
        INNER JOIN #Table1 tb1 ON tb2.PRODCODE = tb1.PRODUCT WHERE
        (
            (
                (tb1.DATE)>=curdate()-90
            )
            AND
            (
                (tb1.type)='I11' Or (tb1.type)=' 11'
            )
            AND
            (
                (tb2.POWNER)='$custCode'
            )
        )
        GROUP BY tb1.PRODUCT, tb2.PRODCODE, tb2.pcustkey 
    )"90Days"
ORDER BY [90Days].[90DayThroughput] DESC