Getting sum result for last year divided quarterly












0















I am trying to get financial data grouped by each quarter of last year and also grouped by the last three weeks.



Also, if it is possible, I want to have the type as a separate column with the type as the header.



This is the query I am using right now which is giving the result for last month.



Select 
to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM')
|| '-'
|| to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY') AS Month,

case
when (ap.asset = 2 or AP.ASSET_TYPE not like 'CFTD%' and
AP.ASSET_TYPE not like 'CASH%' and AP.ASSET_TYPE != 'FTL') then
'AUM'
when (AP.ASSET_TYPE like 'CFTD%') then
'Depostis'
when (AP.ASSET_TYPE like 'CASH%') then
'Cash'
when (AP.ASSET_TYPE = 'FTL') then
'Loan'
end as Type,

sum(ABS(AP.Eval_Market_Value)) as Total

from (select p.account,
p.open_date as Open_Date,
ac.description as RM,
s.*

FROM k$portfolio p, k$client k
LEFT JOIN k$client_role cr
ON cr.client = k.id
AND cr.role = 136
LEFT JOIN k$actors ac
ON cr.actor = ac.id,
table(gtw$reporting.Statement(p.id,
2,
trunc(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1)),
trunc(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1)),
1,
1002,
1,
'USD')) s
wHERE s.line_type = 'P'
and k.id = p.client
and p.id = s.portfolio
and p.portfolio_type = 'C'
and p.status = 1

) ap


group by to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM') ||'-'|| to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY'),

case
when (ap.asset = 2 or AP.ASSET_TYPE not like 'CFTD%' and
AP.ASSET_TYPE not like 'CASH%' and AP.ASSET_TYPE != 'FTL') then
'AUM'
when (AP.ASSET_TYPE like 'CFTD%') then
'Depostis'
when (AP.ASSET_TYPE like 'CASH%') then
'Cash'
when (AP.ASSET_TYPE = 'FTL') then
'Loan'
end


=====================



Current output



Output



Desired result
Final Resualt










share|improve this question
















bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    0















    I am trying to get financial data grouped by each quarter of last year and also grouped by the last three weeks.



    Also, if it is possible, I want to have the type as a separate column with the type as the header.



    This is the query I am using right now which is giving the result for last month.



    Select 
    to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM')
    || '-'
    || to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY') AS Month,

    case
    when (ap.asset = 2 or AP.ASSET_TYPE not like 'CFTD%' and
    AP.ASSET_TYPE not like 'CASH%' and AP.ASSET_TYPE != 'FTL') then
    'AUM'
    when (AP.ASSET_TYPE like 'CFTD%') then
    'Depostis'
    when (AP.ASSET_TYPE like 'CASH%') then
    'Cash'
    when (AP.ASSET_TYPE = 'FTL') then
    'Loan'
    end as Type,

    sum(ABS(AP.Eval_Market_Value)) as Total

    from (select p.account,
    p.open_date as Open_Date,
    ac.description as RM,
    s.*

    FROM k$portfolio p, k$client k
    LEFT JOIN k$client_role cr
    ON cr.client = k.id
    AND cr.role = 136
    LEFT JOIN k$actors ac
    ON cr.actor = ac.id,
    table(gtw$reporting.Statement(p.id,
    2,
    trunc(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1)),
    trunc(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1)),
    1,
    1002,
    1,
    'USD')) s
    wHERE s.line_type = 'P'
    and k.id = p.client
    and p.id = s.portfolio
    and p.portfolio_type = 'C'
    and p.status = 1

    ) ap


    group by to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM') ||'-'|| to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY'),

    case
    when (ap.asset = 2 or AP.ASSET_TYPE not like 'CFTD%' and
    AP.ASSET_TYPE not like 'CASH%' and AP.ASSET_TYPE != 'FTL') then
    'AUM'
    when (AP.ASSET_TYPE like 'CFTD%') then
    'Depostis'
    when (AP.ASSET_TYPE like 'CASH%') then
    'Cash'
    when (AP.ASSET_TYPE = 'FTL') then
    'Loan'
    end


    =====================



    Current output



    Output



    Desired result
    Final Resualt










    share|improve this question
















    bumped to the homepage by Community 1 hour ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      0












      0








      0








      I am trying to get financial data grouped by each quarter of last year and also grouped by the last three weeks.



      Also, if it is possible, I want to have the type as a separate column with the type as the header.



      This is the query I am using right now which is giving the result for last month.



      Select 
      to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM')
      || '-'
      || to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY') AS Month,

      case
      when (ap.asset = 2 or AP.ASSET_TYPE not like 'CFTD%' and
      AP.ASSET_TYPE not like 'CASH%' and AP.ASSET_TYPE != 'FTL') then
      'AUM'
      when (AP.ASSET_TYPE like 'CFTD%') then
      'Depostis'
      when (AP.ASSET_TYPE like 'CASH%') then
      'Cash'
      when (AP.ASSET_TYPE = 'FTL') then
      'Loan'
      end as Type,

      sum(ABS(AP.Eval_Market_Value)) as Total

      from (select p.account,
      p.open_date as Open_Date,
      ac.description as RM,
      s.*

      FROM k$portfolio p, k$client k
      LEFT JOIN k$client_role cr
      ON cr.client = k.id
      AND cr.role = 136
      LEFT JOIN k$actors ac
      ON cr.actor = ac.id,
      table(gtw$reporting.Statement(p.id,
      2,
      trunc(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1)),
      trunc(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1)),
      1,
      1002,
      1,
      'USD')) s
      wHERE s.line_type = 'P'
      and k.id = p.client
      and p.id = s.portfolio
      and p.portfolio_type = 'C'
      and p.status = 1

      ) ap


      group by to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM') ||'-'|| to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY'),

      case
      when (ap.asset = 2 or AP.ASSET_TYPE not like 'CFTD%' and
      AP.ASSET_TYPE not like 'CASH%' and AP.ASSET_TYPE != 'FTL') then
      'AUM'
      when (AP.ASSET_TYPE like 'CFTD%') then
      'Depostis'
      when (AP.ASSET_TYPE like 'CASH%') then
      'Cash'
      when (AP.ASSET_TYPE = 'FTL') then
      'Loan'
      end


      =====================



      Current output



      Output



      Desired result
      Final Resualt










      share|improve this question
















      I am trying to get financial data grouped by each quarter of last year and also grouped by the last three weeks.



      Also, if it is possible, I want to have the type as a separate column with the type as the header.



      This is the query I am using right now which is giving the result for last month.



      Select 
      to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM')
      || '-'
      || to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY') AS Month,

      case
      when (ap.asset = 2 or AP.ASSET_TYPE not like 'CFTD%' and
      AP.ASSET_TYPE not like 'CASH%' and AP.ASSET_TYPE != 'FTL') then
      'AUM'
      when (AP.ASSET_TYPE like 'CFTD%') then
      'Depostis'
      when (AP.ASSET_TYPE like 'CASH%') then
      'Cash'
      when (AP.ASSET_TYPE = 'FTL') then
      'Loan'
      end as Type,

      sum(ABS(AP.Eval_Market_Value)) as Total

      from (select p.account,
      p.open_date as Open_Date,
      ac.description as RM,
      s.*

      FROM k$portfolio p, k$client k
      LEFT JOIN k$client_role cr
      ON cr.client = k.id
      AND cr.role = 136
      LEFT JOIN k$actors ac
      ON cr.actor = ac.id,
      table(gtw$reporting.Statement(p.id,
      2,
      trunc(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1)),
      trunc(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1)),
      1,
      1002,
      1,
      'USD')) s
      wHERE s.line_type = 'P'
      and k.id = p.client
      and p.id = s.portfolio
      and p.portfolio_type = 'C'
      and p.status = 1

      ) ap


      group by to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM') ||'-'|| to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY'),

      case
      when (ap.asset = 2 or AP.ASSET_TYPE not like 'CFTD%' and
      AP.ASSET_TYPE not like 'CASH%' and AP.ASSET_TYPE != 'FTL') then
      'AUM'
      when (AP.ASSET_TYPE like 'CFTD%') then
      'Depostis'
      when (AP.ASSET_TYPE like 'CASH%') then
      'Cash'
      when (AP.ASSET_TYPE = 'FTL') then
      'Loan'
      end


      =====================



      Current output



      Output



      Desired result
      Final Resualt







      oracle oracle-10g group-by sum






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 1 '15 at 18:44









      Joel Brown

      10.4k21837




      10.4k21837










      asked Feb 1 '15 at 7:43









      YousefYousef

      83




      83





      bumped to the homepage by Community 1 hour ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 1 hour ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I'm not sure if I understood the problem correctly, but maybe you're looking for something like this?



          Select 
          to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM') ||'-'|| to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY') AS Month,
          sum(case when (AP.ASSET_TYPE like 'CFTD%') then ABS(AP.Eval_Market_Value) else 0 end) as 'Depostis',
          sum(case when (AP.ASSET_TYPE like 'CASH%') then ABS(AP.Eval_Market_Value) else 0 end) as 'Cash',
          sum(case when (AP.ASSET_TYPE = 'FTL') then ABS(AP.Eval_Market_Value) else 0 end) as 'Loan',
          ...





          share|improve this answer
























          • For the last 3 weeks, you'll probably need a separate query

            – James Z
            Feb 1 '15 at 14:59











          • Thanks for the code JamesZ, that is exactly how I wanted. I need away to query the data for end of each quarter of last year without doing a separate query. and another query for the last 3 weeks

            – Yousef
            Feb 3 '15 at 4:56













          • I need to modify the code to return the result of end of each quarter.

            – Yousef
            Feb 5 '15 at 7:17











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f90730%2fgetting-sum-result-for-last-year-divided-quarterly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I'm not sure if I understood the problem correctly, but maybe you're looking for something like this?



          Select 
          to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM') ||'-'|| to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY') AS Month,
          sum(case when (AP.ASSET_TYPE like 'CFTD%') then ABS(AP.Eval_Market_Value) else 0 end) as 'Depostis',
          sum(case when (AP.ASSET_TYPE like 'CASH%') then ABS(AP.Eval_Market_Value) else 0 end) as 'Cash',
          sum(case when (AP.ASSET_TYPE = 'FTL') then ABS(AP.Eval_Market_Value) else 0 end) as 'Loan',
          ...





          share|improve this answer
























          • For the last 3 weeks, you'll probably need a separate query

            – James Z
            Feb 1 '15 at 14:59











          • Thanks for the code JamesZ, that is exactly how I wanted. I need away to query the data for end of each quarter of last year without doing a separate query. and another query for the last 3 weeks

            – Yousef
            Feb 3 '15 at 4:56













          • I need to modify the code to return the result of end of each quarter.

            – Yousef
            Feb 5 '15 at 7:17
















          0














          I'm not sure if I understood the problem correctly, but maybe you're looking for something like this?



          Select 
          to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM') ||'-'|| to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY') AS Month,
          sum(case when (AP.ASSET_TYPE like 'CFTD%') then ABS(AP.Eval_Market_Value) else 0 end) as 'Depostis',
          sum(case when (AP.ASSET_TYPE like 'CASH%') then ABS(AP.Eval_Market_Value) else 0 end) as 'Cash',
          sum(case when (AP.ASSET_TYPE = 'FTL') then ABS(AP.Eval_Market_Value) else 0 end) as 'Loan',
          ...





          share|improve this answer
























          • For the last 3 weeks, you'll probably need a separate query

            – James Z
            Feb 1 '15 at 14:59











          • Thanks for the code JamesZ, that is exactly how I wanted. I need away to query the data for end of each quarter of last year without doing a separate query. and another query for the last 3 weeks

            – Yousef
            Feb 3 '15 at 4:56













          • I need to modify the code to return the result of end of each quarter.

            – Yousef
            Feb 5 '15 at 7:17














          0












          0








          0







          I'm not sure if I understood the problem correctly, but maybe you're looking for something like this?



          Select 
          to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM') ||'-'|| to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY') AS Month,
          sum(case when (AP.ASSET_TYPE like 'CFTD%') then ABS(AP.Eval_Market_Value) else 0 end) as 'Depostis',
          sum(case when (AP.ASSET_TYPE like 'CASH%') then ABS(AP.Eval_Market_Value) else 0 end) as 'Cash',
          sum(case when (AP.ASSET_TYPE = 'FTL') then ABS(AP.Eval_Market_Value) else 0 end) as 'Loan',
          ...





          share|improve this answer













          I'm not sure if I understood the problem correctly, but maybe you're looking for something like this?



          Select 
          to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'MM') ||'-'|| to_char(add_months(last_day(TO_DATE(&DTE,'YYYYMMDD')),-1), 'YYYY') AS Month,
          sum(case when (AP.ASSET_TYPE like 'CFTD%') then ABS(AP.Eval_Market_Value) else 0 end) as 'Depostis',
          sum(case when (AP.ASSET_TYPE like 'CASH%') then ABS(AP.Eval_Market_Value) else 0 end) as 'Cash',
          sum(case when (AP.ASSET_TYPE = 'FTL') then ABS(AP.Eval_Market_Value) else 0 end) as 'Loan',
          ...






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 1 '15 at 14:50









          James ZJames Z

          2,005821




          2,005821













          • For the last 3 weeks, you'll probably need a separate query

            – James Z
            Feb 1 '15 at 14:59











          • Thanks for the code JamesZ, that is exactly how I wanted. I need away to query the data for end of each quarter of last year without doing a separate query. and another query for the last 3 weeks

            – Yousef
            Feb 3 '15 at 4:56













          • I need to modify the code to return the result of end of each quarter.

            – Yousef
            Feb 5 '15 at 7:17



















          • For the last 3 weeks, you'll probably need a separate query

            – James Z
            Feb 1 '15 at 14:59











          • Thanks for the code JamesZ, that is exactly how I wanted. I need away to query the data for end of each quarter of last year without doing a separate query. and another query for the last 3 weeks

            – Yousef
            Feb 3 '15 at 4:56













          • I need to modify the code to return the result of end of each quarter.

            – Yousef
            Feb 5 '15 at 7:17

















          For the last 3 weeks, you'll probably need a separate query

          – James Z
          Feb 1 '15 at 14:59





          For the last 3 weeks, you'll probably need a separate query

          – James Z
          Feb 1 '15 at 14:59













          Thanks for the code JamesZ, that is exactly how I wanted. I need away to query the data for end of each quarter of last year without doing a separate query. and another query for the last 3 weeks

          – Yousef
          Feb 3 '15 at 4:56







          Thanks for the code JamesZ, that is exactly how I wanted. I need away to query the data for end of each quarter of last year without doing a separate query. and another query for the last 3 weeks

          – Yousef
          Feb 3 '15 at 4:56















          I need to modify the code to return the result of end of each quarter.

          – Yousef
          Feb 5 '15 at 7:17





          I need to modify the code to return the result of end of each quarter.

          – Yousef
          Feb 5 '15 at 7:17


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f90730%2fgetting-sum-result-for-last-year-divided-quarterly%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          SQL Server 17 - Attemping to backup to remote NAS but Access is denied

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          Restoring from pg_dump with foreign key constraints