2015-07-03 45 views
3

我是Firebird數據庫。通過不工作使用「WITH」子句

我有下面的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 

在此先感謝。

祝好!

維沙爾

+0

你ORDER BY是「太早」或無意義的查詢處理器 –

+0

只有一個公共表表達式的遞歸元件可以通過表達包括的命令,或者,使用'TOP'時請參閱http://stackoverflow.com/questions/3924940/sql-cte-and-order-by-affecting-result-set – Ric

+0

@ric在Firebird中,您可以通過在with子句中進行排序,效果只是不能保證(當查詢計劃中沒有其他步驟導致排序時,可能會保留該排序)。在這種情況下,列表(distinct ...)的出現,在不相關字段上的group by以及幾個聯接都可能導致查詢計劃導致排序,從而消除with中的排序效應。 –

回答

2

我得到了問題火鳥雅虎組解決。

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

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

如果我在主SQL中使用Order By ABC.ORDER_NUMBER,那麼我得到五個記錄我在WITH WITH子句中嘗試的原因。我只需要得到一個連接所有Short_Code字段值的記錄。任何建議? –

+0

在返回TBL_SHORT_CODE公用表表達式之前,使用GROUP BY將所有重複組合起來。看我的編輯。 – Wibbler

+0

問題是ORDER_NUMBER包含10,20,30,40,50值。這就是爲什麼我需要避免Group By子句中的ORDER_NUMBER列。 –

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

如果我刪除,那麼它會給出一個錯誤,「ORDER BY子句中的表達式無效(不包含在聚合函數或GROUP BY子句中)」。 –