Sum a summed field - PL/SQL

Asked

Viewed 436 times

0

I need to make the sum of a field to which a sum was made as the example below:

SELECT A.*, B.* FROM

     (SELECT DTAGENDA, AG.CODITPROD, CD, ROW_NUMBER() OVER(PARTITION BY AG.CODITPROD, CD ORDER BY DTAGENDA) AS RANK,
      sum(QTAGENDA) QTD_AGENDA
      FROM ADM.MAG_T_DW_AGENDAMENTO AG
      INNER JOIN ADM.MAG_T_ARVORE_PRODUTO A ON A.CODITPROD = AG.CODITPROD
      WHERE AG.DTAGENDA >= TO_DATE(SYSDATE)  and AG.DTAGENDA <= TO_DATE(SYSDATE+180)
      AND AG.STATUS NOT IN ('CANCELADO', 'DEVOLVEU POR DIVERGÊNCIA', 'NÃO COMPARECEU', 'DESISTÊNCIA')
      GROUP BY DTAGENDA, AG.CODITPROD, CD
      ORDER BY AG.CODITPROD, CD, DTAGENDA) A,

                   (SELECT SUM(QTAGENDA) AS AGENDA_30
                    FROM ADM.MAG_T_DW_AGENDAMENTO AG
                    INNER JOIN ADM.MAG_T_ARVORE_PRODUTO A ON A.CODITPROD = AG.CODITPROD
                    WHERE DTAGENDA >= TO_DATE(SYSDATE+30)
                    GROUP BY AG.CODITPROD
                    ) B              
WHERE A.RANK = 1

However, the result obtained from the second subselect is far from expected, as shown below. https://i.stack.Imgur.com/guSf2.png

The expected scenario would be a sum of the field A.QTD_AGENDA, but I do not know a syntax that allows the same.

Note: Rollup would not help

  • put the table structure and a SQL Fiddle to help

1 answer

0


Tried to :

SELECT A FROM

     (SELECT DTAGENDA, AG.CODITPROD, CD, ROW_NUMBER() OVER(PARTITION BY AG.CODITPROD, CD ORDER BY DTAGENDA) AS RANK,
      sum(QTAGENDA) QTD_AGENDA,
      SUM(QTAGENDA) OVER(PARTITION BY AG.CODITPROD, CD ORDER BY DTAGENDA) AS AGENDA_30
      FROM ADM.MAG_T_DW_AGENDAMENTO AG
      INNER JOIN ADM.MAG_T_ARVORE_PRODUTO A ON A.CODITPROD = AG.CODITPROD
      WHERE AG.DTAGENDA >= TO_DATE(SYSDATE)  and AG.DTAGENDA <= TO_DATE(SYSDATE+180)
      AND AG.STATUS NOT IN ('CANCELADO', 'DEVOLVEU POR DIVERGÊNCIA', 'NÃO COMPARECEU', 'DESISTÊNCIA')
      GROUP BY DTAGENDA, AG.CODITPROD, CD
      ORDER BY AG.CODITPROD, CD, DTAGENDA) A
WHERE A.RANK = 1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.