【问题标题】:Using Break By for Multiple Fields对多个字段使用 Break By
【发布时间】:2015-12-23 21:45:57
【问题描述】:

我想问一下如何在for each 语句中使用多个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 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.

我的预期输出是:

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

【问题讨论】:

  • 你说的是什么语言? SQL?另外,这个问题不是很清楚。是否要计算具有相同 Car Code 和 Color Code 的行数?
  • 我正在使用progress 4gl / opensge 抱歉没有澄清。我想要的是用相应的颜色显示汽车代码的总量..

标签: break progress-4gl openedge progress-db


【解决方案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) 可能为真而 LAST-OF(carcode) 为假,因此将 AND 更改为 OR。

如果 LAST-OF(carcode) 为真,则 LAST-OF(colorcode) 也为真。

【讨论】:

  • 我尝试了您的建议,但没有奏效。但它有不同的输出。
【解决方案2】:

这样的事情应该可以工作:

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。

【讨论】:

    【解决方案3】:

    当我检查代码时,我忽略了最后一个的使用,而是使用了临时表和缓冲区。

    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.
    

    但如果你们有使用 last-of from break by 的解决方案,请分享..

    谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      相关资源
      最近更新 更多