Use more than one value to use parameter in an Oracle function

Asked

Viewed 28 times

0

Good afternoon, I have the following problem:

I need to perform a function that calculates the average value of a product on a specific date.

SELECT VERIF_VL_CUSTO_MEDIO(PRODUCT.CD_PRODUTO, to_date('31/01/2021','dd/mm/yyyy') FROM PRODUCT;

However I need to execute this query, so I return the values to the last day of each month in the last 12 months.

I tried maybe to create a repeating structure, but I was unsuccessful.

I appreciate all your help

1 answer

0

    --sem testes todavia
    --criação de uma view
    --sequencia retroativa
    SELECT (LEVEL-1)*-1
    FROM   DUAL 
    CONNECT BY LEVEL <= 12;
    
    --gerando datas nisto
    SELECT ADD_MONTHS(TRUNC(SYSDATE,'MM'),(LEVEL-1)*-1) REFEF
    FROM   DUAL 
    CONNECT BY LEVEL <= 12;
    
    --gerando datas ultimos dias
    SELECT LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE,'MM'),(LEVEL-1)*-1)) ULD_DIA_REFEF
    FROM   DUAL 
    CONNECT BY LEVEL <= 12;
    
    --criando a view
    CREATE OR REPLACE VIEW V_VL_ULTIMAS_DATAS AS
    SELECT ADD_MONTHS(TRUNC(SYSDATE,'MM'),(LEVEL-1)*-1) refer,
           LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE,'MM'),(LEVEL-1)*-1)) ULD_DIA_REFEF
    FROM   DUAL 
    CONNECT BY LEVEL <= 12;

    --produto cartesiano proposital
   SELECT VERIF_VL_CUSTO_MEDIO(PRODUTO.CD_PRODUTO,
                               VUD.ULD_DIA_REFEF) CST_MED
   FROM PRODUTO P,V_VL_ULTIMAS_DATAS VUD
   WHERE NOME_PRODUTO = 'XPTO;
    
    
    --poderia generalizar o "12" fazendo um MONTHS_BETWEEN 
    --entre a data atual e a menos data de produtos o 
    --filtro seria aplicado na view

    --editei para retirar a function da view
    --o que pode ser feito é um "join" entre " produtos e 
    --esta view
  • is compiled, used a material and a date in the filter, only q was 30 minutes running and I canceled the query. I was thinking of making a LOOP using the data field, but I don’t know how to do this on Oracle

Browser other questions tagged

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