Accumulate or Sum, Subquery possible, row num.

Posted by Jackie0101 on 15-Apr-2015 17:13

I apologize as I am very inexperienced and struggling. Any help would be appreciated. I am trying create a query with a sum/accumulation that returns based on a date descending and only so may rows. I have tried several things and can't seem to put them all together. If I was writing a SQL query here is how I would write the query. select a.custno, rownum, a.dateeff, a.pid, a.ptotal from (select a.custno custno, a.dateeff dateeff, a.batchid || ' ' || a.docid pid, sum(a.value) ptotal from tran a where a.type = '10' group by a.custno, a.dateeff, a.batchid || ' ' || a.docid ) a where a.custno = '224755' and rownum

Posted by ChUIMonster on 16-Apr-2015 10:34

As MadDBA says -- forget SQL.

Trying to make Progress be SQL only leads to agony and frustration.

I have no idea what your databases schema is so I will use "sports2000".  The important elements that you probably want to become familiar with first are below:

define variable stateCount as integer no-undo.

for each customer no-lock break by customer.state:

  if first-of( customer.state ) then
    stateCount = 0.

  stateCount = stateCount + 1.

  if last-of( custome.state ) then
    display stateCount.

end.

This will provide a list of "states" and the count of customers per state.  There are statements like COUNT and ACCUM that automate some of this but I never use them.  They're finicky and more trouble than they are worth.  IMHO.

All Replies

Posted by James Palmer on 16-Apr-2015 02:22

What have you tried so far in terms of Progress?

Posted by TheMadDBA on 16-Apr-2015 10:21

Step 1: Forget almost everything about SQL :)

Look at the progress documentation for the following

FOR EACH

BREAK-BY

Posted by ChUIMonster on 16-Apr-2015 10:34

As MadDBA says -- forget SQL.

Trying to make Progress be SQL only leads to agony and frustration.

I have no idea what your databases schema is so I will use "sports2000".  The important elements that you probably want to become familiar with first are below:

define variable stateCount as integer no-undo.

for each customer no-lock break by customer.state:

  if first-of( customer.state ) then
    stateCount = 0.

  stateCount = stateCount + 1.

  if last-of( custome.state ) then
    display stateCount.

end.

This will provide a list of "states" and the count of customers per state.  There are statements like COUNT and ACCUM that automate some of this but I never use them.  They're finicky and more trouble than they are worth.  IMHO.

Posted by Jackie0101 on 16-Apr-2015 17:17

First of all, thank you all for replying to me questions. I will try to answer all your questions in one reply. I do agree with everyone that SQL isn’t the way to go, but it was the only way I could really show what I am trying to accomplish. I have tried several things over few weeks and honestly I lost count of all the things I have tried. Below a couple of things I have kept that came close, but they are still missing pieces.
 
What I am trying to pull back is the last 5 payments on a specific account. The account number will be passed to this process and I am really just needing a return of the date and amounts. The full payment is broken down into two fields Batch and Doc. I need the date so that I can sort descending, plus I need to return that information.
 
This is probably the most successful, because it lists all the payment allocations together in descending date order by the batch and doc, but it isn’t adding the totals together. I didn’t go forward with next and last, because I couldn’t get the first part to work.
 
Define temp-table tt-Payment NO-UNDO
FIELD iCustRef            LIKE tran.custno     LABEL "Cust Ref"                  FORMAT "X(8)"
FIELD iPayNo              AS CHARACTER      LABEL "Payment No"
FIELD cBatchDocID    AS CHARACTER      LABEL "Payment Ref"           FORMAT "X(3)"
FIELD iPayAmt           AS DECIMAL           LABEL "Payment Amount"              
FIELD dPayEff            AS DATE                 LABEL "Payment Date"         FORMAT "99/99/9999".
 
FOR EACH tran WHERE tran.type = '10' and tran.custno = '10224755' NO-LOCK BREAK BY tran.dateeff DESC BY tran.batchid + " " + tran.docid:
   CREATE tt-Payment. 
   ASSIGN tt-Payment.iCustRef = tran.custno.
  
   FIND FIRST tt-Payment WHERE tt-Payment.iCustRef = tran.custno No-Error.
  
  IF AVAILABLE tt-Payment THEN DO:
          CREATE tt-Payment.
           ASSIGN tt-Payment.iCustRef = tran.custno
                       tt-Payment.cBatchDocID = tran.batchid + " " + tran.docid
                       tt-Payment.iPayAmt = 0
                       tt-Payment.dPayEff = tran.dateeff.
 
  IF tt-Payment.cBatchDocID = tran.batchid + " " + tran.docid THEN
          ASSIGN tt-Payment.iPayAmt = tt-Payment.iPayAmt + tran.value.
 
DISPLAY tt-Payment.iCustRef
              tt-Payment.cBatchDocID
              tt-Payment.iPayAmt
              tt-Payment.dPayEff
 
--------------------------------------------------
This is the second most successful, because it groups all the payments together by the batch and doc, but it only gives me the first last payment and then it goes back to ascending and again it still gives me each allocation, but it also gives me a total. I only want one line with the total and not the allocation. I suspect that this needs to be wrapped into another query, but I just can’t figure it out.
 
FOR EACH tran WHERE tran.custno = '10224755' and tran.type = '10' BREAK BY tran.batchid + ' ' + tran.docid:
  DISPLAY tran.custno
               tran.dateeff 
               tran.batchid + ' ' + tran.docid
               tran.value (TOTAL BY tran.batchid + ' ' + tran.docid).
END.

Posted by Marian Edu on 17-Apr-2015 01:03

def var totalValue like tran.value. /* damn I hate like, use the correct data type instead */

FOR EACH tran WHERE tran.custno = '10224755' and tran.type = '10' BREAK BY tran.batchid BY tran.docid:
 
 totalValue = totalValue + tran.value.

 if last-of(tran.docid) then do:
   DISPLAY tran.custno
              tran.dateeff
              tran.batchid + ' ' + tran.docid
              totalValue.
   totalValue = 0.
 end.

END.

- See more at: community.progress.com/.../61897.aspx


Posted by Chandra Sekhar on 17-Apr-2015 07:48

SQL does not support “rownum” . Use “ROWID”,  If you are looking for rowid of  each row.

If you are not looking for the row-id , interested in sequential numbers for each row then use sequences.

Posted by Jackie0101 on 17-Apr-2015 09:53

I want to thank everyone. All of this information was very helpful and has given us a new level of understanding. We are still learning, but this one was a GREAT help.
 
 
[collapse]
From: Chandra Sekhar [mailto:bounce-csekhar@community.progress.com]
Sent: Friday, April 17, 2015 7:49 AM
To: TU.OE.General@community.progress.com
Subject: RE: [Technical Users - OE General] Accumulate or Sum, Subquery possible, row num.
 
Reply by Chandra Sekhar

SQL does not support “rownum” . Use “ROWID”,  If you are looking for rowid of  each row.

If you are not looking for the row-id , interested in sequential numbers for each row then use sequences.

Stop receiving emails on this subject.

Flag this post as spam/abuse.

[/collapse]

Posted by Jackie0101 on 17-Apr-2015 09:54

I want to thank everyone. All of this information was very helpful and has given us a new level of understanding. We are still learning, but this one was a GREAT help.

Posted by Jean Richert on 17-Apr-2015 17:48

[mention:825fc31ee1cd4ff681ed68b631c5eced:e9ed411860ed4f2ba0265705b8793d05] Great to hear our Community helped you. Could you flag the question as answered? Thanks.

Also we have an OpenEdge Development group with a forum. I'll move this thread to it and would suggest you could join and post this kind of ABL/Development type of questions. Cheers

Posted by Jean Richert on 17-Apr-2015 17:49

Here the link to the OE Dev group

community.progress.com/.../default.aspx

This thread is closed