2015-09-26 86 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。