2015-09-26 38 views
1

我想問一下如何在for each語句中使用多個break。在多個字段中使用Break By

樣品:

Car Code Color Code 
0001  002 
0001  002 
0001  001 
0005  003 
0005  002 
0007  001 
0008  001 
0008  005 
0008  001 

我的代碼是:

def var ctr as int. 

對於每一車沒鎖打破由carcode通過的ColorCode。

ctr = ctr + 1. 


/*I tried*/ 
    if last-of(carcode) and last-of(colorcode) then do: 
     disp carcode colorcode ctr. 
     ctr = 0. 
    end. 


/*and tried*/ 
    last-of(colorcode) then do: 
     if last-of(carcode) 
     disp carcode colorcode ctr. 
     ctr = 0. 
     end. 
    end. 
end. 

我的預期輸出是:

car code Color Code QTY 
0001  001   1 
0001  002   2 
0005  002   1 
0005  003   1 
0007  001   1 
0008  001   2 
0008  005   1 
+2

你在說什麼語言? SQL?另外,這個問題不是很清楚。你想用相同的汽車代碼和顏色代碼來統計行嗎? –

+0

我正在使用進度4gl /打開抱歉不澄清。我想要的是顯示汽車代碼的總數量與其相應的顏色。 – noob

回答

1

試試這個:

FOR EACH tablename NO-LOCK 
    BREAK BY carcode 
     BY colorcode: 

    ctr = ctr + 1. 

    if last-of(carcode) OR last-of(colorcode) then do: 
     disp carcode colorcode ctr. 
     ctr = 0. 
    end. 
END. 

這是可能的LAST-OF(的ColorCode)是真實的和最後的(carcode)是假的,所以把AND改爲OR。

如果LAST-OF(carcode)爲真,那麼LAST-OF(colorcode)也是如此。

+0

我試過你的建議,但它沒有奏效。它有不同的輸出。 – noob

+1

然後有些東西你沒有告訴我們.... –

0

當我檢查代碼時,我不考慮使用最後一個,而是使用了臨時表和緩衝區。

def buffer btt-car for tt-car. 

find first tt-car where tt-car.carcode = car.carcode exclusive. 
if not avail tt-car then do: 
    create tt-car. 
    assign tt-car.car-code = car.carcode 
    tt-car.color-code = car.colorcode 
    tt-car.qty = tt-car.qty + car.qty. 
end. 
if avail tt-car then do: 
    find first btt-car where btt-car.colorcode = car.colorcode exclusive. 
    if not avail btt-car then do: 
    create btt-car. 
    assign btt-car.car-code = car.carcode 
    btt-car.color-code = car.colorcode 
    btt-car.qty = btt-car.qty + car.qty. 
    end. 
    if avail btt-car then assign btt-car.qty = btt-car.qty + car.qty. 
end. 

,但如果你們有使用最後的突破,從通過的解決方案,請分享..

感謝

0

像這樣的東西應該工作:

for each car 
    no-lock 
    break by car.carcode 
      by car.colorcode 
: 

    accumulate car.colorcode (count by car.colorcode). 

    if last-of(car.colorcode) 
    then 
     display car.carcode 
       car.colorcode 
       (accum count by car.colorcode car.colorcode). 

end. 

你可以當然,如果你想要使用一個變量而不是ACCUMULATE。

相關問題