按不工作使用 "WITH" 子句排序

Order By Not Working Using "WITH" Clause

我正在使用 Firebird 数据库。

我有下面的 SQL,它连接了 ShortCode 列数据,但没有按照 "WITH" 子句中 ABC table 中的 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

提前致谢。

谨致问候。

维沙尔

ORDER BY 在常见的 table 表达式中不起作用,并且您加入 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

尝试关注,

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

我在 Firebird yahoo 组解决了问题。

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