[英]Using Break By for Multiple Fields
I would like to ask how to use multiple break by in a for each
statement.我想问一下如何在
for each
语句中使用多个break by。
Sample:样本:
Car Code Color Code
0001 002
0001 002
0001 001
0005 003
0005 002
0007 001
0008 001
0008 005
0008 001
My code is:我的代码是:
def var ctr as int.
For each car no-lock break by carcode by 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.
My expected output would be:我的预期输出是:
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
Try this:尝试这个:
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.
It is possible for LAST-OF(colorcode) to be true and LAST-OF(carcode) be false, so change the AND to an OR.有可能 LAST-OF(colorcode) 为真而 LAST-OF(carcode) 为假,因此将 AND 更改为 OR。
If LAST-OF(carcode) is true, then LAST-OF(colorcode) will also be true.如果 LAST-OF(carcode) 为真,则 LAST-OF(colorcode) 也为真。
as I was checking on the code, i disregard the use of the last-of and used a temp-table and buffer instead.当我检查代码时,我忽略了 last-of 的使用,而是使用了临时表和缓冲区。
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.
but if you guys have solutions with using last-of from break by, please share..但如果你们有使用 last-of from break by 的解决方案,请分享..
thanks谢谢
Something like this should work:这样的事情应该工作:
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.
You can use a variable instead of ACCUMULATE if you want, of course.当然,如果需要,您可以使用变量代替 ACCUMULATE。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.