不使用“WITH”条款的命令

时间:2015-07-03 14:12:30

标签: sql firebird

我是Firebird数据库。

我有以下SQL,它连接ShortCode列数据,但没有根据“WITH”子句中ABC表中的ORDER_NUMBER列进行排序。

With TBL_SHORT_CODE (SHORT_CODE, FK_KEY) As 
(
SELECT Distinct(XYZ.SHORT_CODE) As SHORT_CODE, ABC.FK_KEY 
From ABC Join XYZ On ABC.PK_KEY = XYZ.FK_KEY
where XYZ.FK_KEY = '{009DA0F8-51EE-4207-86A6-7E18F96B983A}' And ABC.STATUS_CODE = 1
Order By ABC.ORDER_NUMBER
)


SELECT LIST(Distinct(TBL_SHORT_CODE.SHORT_CODE), '' ), ABC.FK_BOM
From ABC 
Join XYZ ON ABC.FK_KEY = XYZ.PK_KEY 
Join TBL_SHORT_CODE On TBL_SHORT_CODE.FK_KEY = ABC.FK_KEY
where ABC.FK_BOM = '{009DA0F8-51EE-4207-86A6-7E18F96B983A}' And ABC.STATUS_CODE = 1
Group By ABC.FK_BOM

提前致谢。

最诚挚的问候。

维沙尔

3 个答案:

答案 0 :(得分:2)

ORDER BY在公用表表达式中不起作用,并且您对TBL_SHORT_CODE的连接无论如何都不会从中获益。如果您的订购只是订购输出,那么请更改您的脚本以在最终查询中订购:

With TBL_SHORT_CODE (SHORT_CODE, FK_KEY) As 
(
SELECT XYZ.SHORT_CODE As SHORT_CODE, ABC.FK_KEY
From ABC Join XYZ On ABC.PK_KEY = XYZ.FK_KEY
where XYZ.FK_KEY = '{009DA0F8-51EE-4207-86A6-7E18F96B983A}' And ABC.STATUS_CODE = 1
GROUP BY XYZ.SHORT_CODE, ABC.FK_KEY
)


SELECT LIST(Distinct(TBL_SHORT_CODE.SHORT_CODE), '' ), ABC.FK_BOM
From ABC 
Join XYZ ON ABC.FK_KEY = XYZ.PK_KEY 
Join TBL_SHORT_CODE On TBL_SHORT_CODE.FK_KEY = ABC.FK_KEY
where ABC.FK_BOM = '{009DA0F8-51EE-4207-86A6-7E18F96B983A}' And     ABC.STATUS_CODE = 1
Group By ABC.FK_BOM

答案 1 :(得分:2)

我在Firebird雅虎集团解决了问题。

SQL:

EXECUTE BLOCK RETURNS (SHORT_CODES VARCHAR(2000), FK_BOM INTEGER
AS 
DECLARE VARIABLE SHORT_CODE1 VARCHAR(2000);
DECLARE VARIABLE FK_BOM2 INTEGER;
DECLARE VARIABLE DUMMY INTEGER;
BEGIN
    FK_BOM = NULL;
    FOR With TBL_SHORT_CODE (SHORT_CODE, FK_KEY, ORDER_NUMBER) As 
        (SELECT XYZ.SHORT_CODE, ABC.FK_KEY, min(ABC.ORDER_NUMBER)
        From ABC Join XYZ On ABC.PK_KEY = XYZ.FK_KEY
        where XYZ.FK_KEY = '{009DA0F8-51EE-4207-86A6-7E18F96B983A}' And ABC.STATUS_CODE = 1
        group by 1, 2)

    SELECT ABC.FK_BOM, tsc.SHORT_CODE, min(tsc.ORDER_NUMBER)
    From ABC 
    Join XYZ ON ABC.FK_KEY = XYZ.PK_KEY 
    Join TBL_SHORT_CODE tsc On tsc.FK_KEY = ABC.FK_KEY
    where ABC.FK_BOM = '{009DA0F 8-51EE-4207-86A6-7E18F96B983A}' And ABC.STATUS_CODE = 1
    Group By 1, 2
    ORDER BY 1, 3
    into :FK_BOM2, :SHORT_CODE1, :DUMMY do
    begin
        if (FK_BOM2 > FK_BOM) then
            suspend;
        if (FK_BOM2 is distinct from FK_BOM) then
        begin
            FK_BOM = FK_BOM2;
            SHORT_CODES = '';
        end
        SHORT_CODES = SHORT_CODES || SHORT_CODE1;
    end
    suspend;
end

答案 2 :(得分:0)

尝试以下,

with TBL_SHORT_CODE (SHORT_CODE, FK_KEY, FK_BOM, ORDER_NUMBER) As 
(
     SELECT Distinct(XYZ.SHORT_CODE) As SHORT_CODE
          , ABC.FK_KEY, ABC.FK_BOM, ABC.ORDER_NUMBER
       From ABC Join XYZ On ABC.PK_KEY = XYZ.FK_KEY
      where XYZ.FK_KEY = '{009DA0F8-51EE-4207-86A6-7E18F96B983A}'
        And ABC.STATUS_CODE = 1
)


SELECT LIST(Distinct(TBL_SHORT_CODE.SHORT_CODE), '' ), A.FK_BOM
  From TBL_SHORT_CODE A
 where A.FK_BOM = '{009DA0F8-51EE-4207-86A6-7E18F96B983A}'
   And ABC.STATUS_CODE = 1
 Group By A.FK_BOM
 ORDER BY TBL_SHORT_CODE.ORDER_NUMBER