SQL Calculate Yearly Average Balance for ForecastHow can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableT-SQL average calculationHow to import an SQL file using the command line in MySQL?Calculating difference between daily sum and a average for the same day of the week in defined time range. SQL 10g Oracle

Is camera lens focus an exact point or a range?

Folder comparison

Why in book's example is used 言葉(ことば) instead of 言語(げんご)?

How to color a curve

Reply 'no position' while the job posting is still there

How much character growth crosses the line into breaking the character

Proving a function is onto where f(x)=|x|.

Did US corporations pay demonstrators in the German demonstrations against article 13?

A social experiment. What is the worst that can happen?

Will adding a BY-SA image to a blog post make the entire post BY-SA?

How do I repair my stair bannister?

Difference between -| and |- in TikZ

Why does the integral domain "being trapped between a finite field extension" implies that it is a field?

Is it possible to use .desktop files to open local pdf files on specific pages with a browser?

What (else) happened July 1st 1858 in London?

THT: What is a squared annular “ring”?

Should I stop contributing to retirement accounts?

Transformation of random variables and joint distributions

Divine apple island

How will losing mobility of one hand affect my career as a programmer?

Flux received by a negative charge

Should I install hardwood flooring or cabinets first?

Is XSS in canonical link possible?

What is the gram­mat­i­cal term for “‑ed” words like these?



SQL Calculate Yearly Average Balance for Forecast


How can I prevent SQL injection in PHP?How do I perform an IF…THEN in an SQL SELECT?Add a column with a default value to an existing table in SQL ServerHow to return only the Date from a SQL Server DateTime datatypeInserting multiple rows in a single SQL query?How do I UPDATE from a SELECT in SQL Server?Finding duplicate values in a SQL tableT-SQL average calculationHow to import an SQL file using the command line in MySQL?Calculating difference between daily sum and a average for the same day of the week in defined time range. SQL 10g Oracle













0















I'm trying to find a more efficient way to find the Yearly Average Balance for Accounts in a SQL table.



The number of days for each period are listed below, is there a more efficient way to code this? The #TEMPAVG table itself is a pivot of fields with Period containing the data. (Periods on Columns with #'s, Rows contain all other fields)



Yearly average Balance is calculated by Summarizing each time period, multiplying by # of days, and then dividing by total number of days.



I attempted to insert these Day values into a #TEMPDAYS table but couldn't figure out how to join the data correctly for multiplication of time periods.



TEMP DAYS Table



 INSERT INTO #TEMPDAYS(Period,Days)
VALUES
('P01','30'),('P02','31'),('P03','31'),('P04','28'),('P05','31'),('P06','30'),('P07','31'),('P08','30'),('P09','31'),('P10','31'),('P11','30'),('P12','31')
,('Jan','31'),('Feb','28'),('Mar','31'),('Apr','30'),('May','31'),('Jun','30'),('Jul','31'),('Aug','31'),('Sep','30'),('Oct','31'),('Nov','30'),('Dec','31');


Days for Period Avgs



-- DAYS FOR EACH PERIOD ('P01','30'),('P02','31'),('P03','31'),('P04','28'),('P05','31'),('P06','30'),('P07','31'),('P08','30'),('P09','31'),('P10','31'),('P11','30'),('P12','31')


Yearly Average Balance Calculation for Last Time Period P12



SELECT 
PERIOD='P12'
,[Account],[BOA],[OrgUnit],[AverageBalance]='YAB',[AmountType]='I',[Years],[Currency],[Scenario],[Version],[IntExt]
,ROUND(
(
SUM(P01)*30 + SUM(P02)*31 + SUM(P03)*31 +
SUM(P04)*28 + SUM(P05)*31 + SUM(P06)*30 +
SUM(P07)*28 + SUM(P08)*31 + SUM(P09)*30 +
SUM(P10)*28 + SUM(P11)*31 + SUM(P12)*30
)
/(30 + 31 + 31 +
28 + 31 + 30 +
31 + 30 + 31 +
31 + 30 + 31
)
,2)
AS DATA
,[DataSource]= [DataSource]+' Avg'
FROM #TEMPAVG
GROUP BY [Account],[BOA],[OrgUnit],[Years],[Currency],[Scenario],[Version],[IntExt],[DATASOURCE]









share|improve this question


























    0















    I'm trying to find a more efficient way to find the Yearly Average Balance for Accounts in a SQL table.



    The number of days for each period are listed below, is there a more efficient way to code this? The #TEMPAVG table itself is a pivot of fields with Period containing the data. (Periods on Columns with #'s, Rows contain all other fields)



    Yearly average Balance is calculated by Summarizing each time period, multiplying by # of days, and then dividing by total number of days.



    I attempted to insert these Day values into a #TEMPDAYS table but couldn't figure out how to join the data correctly for multiplication of time periods.



    TEMP DAYS Table



     INSERT INTO #TEMPDAYS(Period,Days)
    VALUES
    ('P01','30'),('P02','31'),('P03','31'),('P04','28'),('P05','31'),('P06','30'),('P07','31'),('P08','30'),('P09','31'),('P10','31'),('P11','30'),('P12','31')
    ,('Jan','31'),('Feb','28'),('Mar','31'),('Apr','30'),('May','31'),('Jun','30'),('Jul','31'),('Aug','31'),('Sep','30'),('Oct','31'),('Nov','30'),('Dec','31');


    Days for Period Avgs



    -- DAYS FOR EACH PERIOD ('P01','30'),('P02','31'),('P03','31'),('P04','28'),('P05','31'),('P06','30'),('P07','31'),('P08','30'),('P09','31'),('P10','31'),('P11','30'),('P12','31')


    Yearly Average Balance Calculation for Last Time Period P12



    SELECT 
    PERIOD='P12'
    ,[Account],[BOA],[OrgUnit],[AverageBalance]='YAB',[AmountType]='I',[Years],[Currency],[Scenario],[Version],[IntExt]
    ,ROUND(
    (
    SUM(P01)*30 + SUM(P02)*31 + SUM(P03)*31 +
    SUM(P04)*28 + SUM(P05)*31 + SUM(P06)*30 +
    SUM(P07)*28 + SUM(P08)*31 + SUM(P09)*30 +
    SUM(P10)*28 + SUM(P11)*31 + SUM(P12)*30
    )
    /(30 + 31 + 31 +
    28 + 31 + 30 +
    31 + 30 + 31 +
    31 + 30 + 31
    )
    ,2)
    AS DATA
    ,[DataSource]= [DataSource]+' Avg'
    FROM #TEMPAVG
    GROUP BY [Account],[BOA],[OrgUnit],[Years],[Currency],[Scenario],[Version],[IntExt],[DATASOURCE]









    share|improve this question
























      0












      0








      0








      I'm trying to find a more efficient way to find the Yearly Average Balance for Accounts in a SQL table.



      The number of days for each period are listed below, is there a more efficient way to code this? The #TEMPAVG table itself is a pivot of fields with Period containing the data. (Periods on Columns with #'s, Rows contain all other fields)



      Yearly average Balance is calculated by Summarizing each time period, multiplying by # of days, and then dividing by total number of days.



      I attempted to insert these Day values into a #TEMPDAYS table but couldn't figure out how to join the data correctly for multiplication of time periods.



      TEMP DAYS Table



       INSERT INTO #TEMPDAYS(Period,Days)
      VALUES
      ('P01','30'),('P02','31'),('P03','31'),('P04','28'),('P05','31'),('P06','30'),('P07','31'),('P08','30'),('P09','31'),('P10','31'),('P11','30'),('P12','31')
      ,('Jan','31'),('Feb','28'),('Mar','31'),('Apr','30'),('May','31'),('Jun','30'),('Jul','31'),('Aug','31'),('Sep','30'),('Oct','31'),('Nov','30'),('Dec','31');


      Days for Period Avgs



      -- DAYS FOR EACH PERIOD ('P01','30'),('P02','31'),('P03','31'),('P04','28'),('P05','31'),('P06','30'),('P07','31'),('P08','30'),('P09','31'),('P10','31'),('P11','30'),('P12','31')


      Yearly Average Balance Calculation for Last Time Period P12



      SELECT 
      PERIOD='P12'
      ,[Account],[BOA],[OrgUnit],[AverageBalance]='YAB',[AmountType]='I',[Years],[Currency],[Scenario],[Version],[IntExt]
      ,ROUND(
      (
      SUM(P01)*30 + SUM(P02)*31 + SUM(P03)*31 +
      SUM(P04)*28 + SUM(P05)*31 + SUM(P06)*30 +
      SUM(P07)*28 + SUM(P08)*31 + SUM(P09)*30 +
      SUM(P10)*28 + SUM(P11)*31 + SUM(P12)*30
      )
      /(30 + 31 + 31 +
      28 + 31 + 30 +
      31 + 30 + 31 +
      31 + 30 + 31
      )
      ,2)
      AS DATA
      ,[DataSource]= [DataSource]+' Avg'
      FROM #TEMPAVG
      GROUP BY [Account],[BOA],[OrgUnit],[Years],[Currency],[Scenario],[Version],[IntExt],[DATASOURCE]









      share|improve this question














      I'm trying to find a more efficient way to find the Yearly Average Balance for Accounts in a SQL table.



      The number of days for each period are listed below, is there a more efficient way to code this? The #TEMPAVG table itself is a pivot of fields with Period containing the data. (Periods on Columns with #'s, Rows contain all other fields)



      Yearly average Balance is calculated by Summarizing each time period, multiplying by # of days, and then dividing by total number of days.



      I attempted to insert these Day values into a #TEMPDAYS table but couldn't figure out how to join the data correctly for multiplication of time periods.



      TEMP DAYS Table



       INSERT INTO #TEMPDAYS(Period,Days)
      VALUES
      ('P01','30'),('P02','31'),('P03','31'),('P04','28'),('P05','31'),('P06','30'),('P07','31'),('P08','30'),('P09','31'),('P10','31'),('P11','30'),('P12','31')
      ,('Jan','31'),('Feb','28'),('Mar','31'),('Apr','30'),('May','31'),('Jun','30'),('Jul','31'),('Aug','31'),('Sep','30'),('Oct','31'),('Nov','30'),('Dec','31');


      Days for Period Avgs



      -- DAYS FOR EACH PERIOD ('P01','30'),('P02','31'),('P03','31'),('P04','28'),('P05','31'),('P06','30'),('P07','31'),('P08','30'),('P09','31'),('P10','31'),('P11','30'),('P12','31')


      Yearly Average Balance Calculation for Last Time Period P12



      SELECT 
      PERIOD='P12'
      ,[Account],[BOA],[OrgUnit],[AverageBalance]='YAB',[AmountType]='I',[Years],[Currency],[Scenario],[Version],[IntExt]
      ,ROUND(
      (
      SUM(P01)*30 + SUM(P02)*31 + SUM(P03)*31 +
      SUM(P04)*28 + SUM(P05)*31 + SUM(P06)*30 +
      SUM(P07)*28 + SUM(P08)*31 + SUM(P09)*30 +
      SUM(P10)*28 + SUM(P11)*31 + SUM(P12)*30
      )
      /(30 + 31 + 31 +
      28 + 31 + 30 +
      31 + 30 + 31 +
      31 + 30 + 31
      )
      ,2)
      AS DATA
      ,[DataSource]= [DataSource]+' Avg'
      FROM #TEMPAVG
      GROUP BY [Account],[BOA],[OrgUnit],[Years],[Currency],[Scenario],[Version],[IntExt],[DATASOURCE]






      sql average essbase






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 21 at 13:55









      MikeMike

      215




      215






















          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f55282050%2fsql-calculate-yearly-average-balance-for-forecast%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f55282050%2fsql-calculate-yearly-average-balance-for-forecast%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

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현