SQL Summing all values, how to break by blocks

Asked

Viewed 59 times

0

I have a table that stores financial values per month, I need to display by year, I made a SUM and it worked when I have only one project registered, when I have more than one project it sums up all the values of all the years and replicates in all the projects:

The query is below, The bank is Oracle

SELECT proj.request_id Num_projeto,
       proj.project_name Nome_Projeto,
       RD.VISIBLE_PARAMETER23 Run_Rate_Aumenta_rec,
       RD.VISIBLE_PARAMETER25 R$,
       RD.VISIBLE_PARAMETER26 Porc,
       RD.VISIBLE_PARAMETER28 Run_Rate_Red_custo,
       RD.VISIBLE_PARAMETER30 R$,
       RD.VISIBLE_PARAMETER31 Porc,
       (SELECT 'R$' || SUM(bp.planned_value_bse) Valor_Benefício
          FROM KCRT_REQUEST_DETAILS RD
         INNER JOIN KCRT_FG_PFM_PROJECT PROJ
            on proj.request_id = rd.request_id
         INNER JOIN PFM_LIFECYCLE_PARENT_ENTITY LC
            on lc.project_req_id = proj.request_id
         INNER JOIN FM_FINANCIAL_SUMMARY FS
            on fs.parent_id = lc.lifecycle_id
         INNER JOIN FM_BENEFIT_PERIOD_SUM BP
            on fs.benefit_id = bp.benefit_id
         INNER JOIN PPM_FISCAL_PERIODS_NLS FP
            on fp.fiscal_period_id = bp.period_id
        --WHERE proj.request_id = '31539'
         WHERE RD.BATCH_NUMBER = 1
           AND fp.fiscal_period_id in ('35967',
                                       '35974',
                                       '35962',
                                       '35956',
                                       '35980',
                                       '35997',
                                       '35985',
                                       '36002',
                                       '35945',
                                       '35992',
                                       '35940',
                                       '35950')) as BDEZESETE,
       (SELECT 'R$' || SUM(bp.planned_value_bse) Valor_Benefício
          FROM KCRT_REQUEST_DETAILS RD
         INNER JOIN KCRT_FG_PFM_PROJECT PROJ
            on proj.request_id = rd.request_id
         INNER JOIN PFM_LIFECYCLE_PARENT_ENTITY LC
            on lc.project_req_id = proj.request_id
         INNER JOIN FM_FINANCIAL_SUMMARY FS
            on fs.parent_id = lc.lifecycle_id
         INNER JOIN FM_BENEFIT_PERIOD_SUM BP
            on fs.benefit_id = bp.benefit_id
         INNER JOIN PPM_FISCAL_PERIODS_NLS FP
            on fp.fiscal_period_id = bp.period_id
        --WHERE proj.request_id = '31539'
         WHERE RD.BATCH_NUMBER = 1
           AND fp.fiscal_period_id in ('36073',
                                       '36068',
                                       '36038',
                                       '36021',
                                       '36046',
                                       '36016',
                                       '36051',
                                       '36033',
                                       '36011',
                                       '36056',
                                       '36028',
                                       '36063')) as BDEZOITO
  FROM KCRT_REQUEST_DETAILS RD
 INNER JOIN KCRT_FG_PFM_PROJECT PROJ
    on proj.request_id = rd.request_id
 INNER JOIN PFM_LIFECYCLE_PARENT_ENTITY LC
    on lc.project_req_id = proj.request_id
 INNER JOIN FM_FINANCIAL_SUMMARY FS
    on fs.parent_id = lc.lifecycle_id
 INNER JOIN FM_BENEFIT_PERIOD_SUM BP
    on fs.benefit_id = bp.benefit_id
 INNER JOIN PPM_FISCAL_PERIODS_NLS FP
    on fp.fiscal_period_id = bp.period_id
 WHERE proj.request_id = '31539'
   AND RD.BATCH_NUMBER = 1
 Group by proj.request_id,
          proj.project_name,
          proj.project_name,
          RD.VISIBLE_PARAMETER23,
          RD.VISIBLE_PARAMETER25,
          RD.VISIBLE_PARAMETER26,
          RD.VISIBLE_PARAMETER28,
          RD.VISIBLE_PARAMETER30,
          RD.VISIBLE_PARAMETER31
  • The "year" is a period_id ? Better indent an sql of this size without indentation and without knowing the basis is difficult , I seem to have missed linking the project_id in Queries than I believe the total per year.

1 answer

0


The problem was solved by inserting a Join that "ties" the request id so that it disappears line by line.

I also had to replace Inner Join with a left Join.

Thanks for the help!

Browser other questions tagged

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